Java I';我正在学习如何开发应用程序,我不知道';我不知道为什么我的应用程序崩溃了
这些是logcat错误(我不确定这是否有助于您回答) 因为我只编辑了下面的java代码和xml代码,所以我只添加了这些代码 JAVA代码 ''' ''' mxl代码Java I';我正在学习如何开发应用程序,我不知道';我不知道为什么我的应用程序崩溃了,java,android,xml,android-studio,Java,Android,Xml,Android Studio,这些是logcat错误(我不确定这是否有助于您回答) 因为我只编辑了下面的java代码和xml代码,所以我只添加了这些代码 JAVA代码 ''' ''' mxl代码 package com.song.androidprogramingbasic; import android.content.DialogInterface; import android.content.Intent; import android.media.MediaPlayer; import android.net.Ur
package com.song.androidprogramingbasic;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
MediaPlayer mp = MediaPlayer.create(this, R.raw.alarm_sound);
@Override
protected void onCreate(Bundle savedInstanceState){ /* 가장 먼저 실행되는 함수*/
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView title = (TextView) findViewById(R.id.first_screen);
title.setText(R.string.Hello_INU);
}
public void Button1(View view){
Intent intent = new Intent(MainActivity.this, Button1Activity.class);
startActivity(intent); /*Intent객체를 받아서, intent객체의 정보로 뭔가를 한다고 함*/
}
public void Button2(View view){
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:010-1234-5678"));
startActivity(intent); /*Intent객체를 받아서, intent객체의 정보로 뭔가를 한다고 함*/
}
public void alert_Dialog(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.button4_text);
builder.setTitle(R.string.button4_title);
builder.setPositiveButton(R.string.button4_dismiss, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert_dialog = builder.create();
alert_dialog.show();
}
public void alarm_sound(View view){
mp.seekTo(0);
mp.start();
}
public void on_Long_Click(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.button8_text);
builder.setTitle(R.string.button8_title);
builder.setPositiveButton(R.string.button8_dismiss, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert_dialog = builder.create();
alert_dialog.show();
}
}
是,因为在创建到
MediaPalyer.create(Context,resID)
尝试在onCreate
方法中初始化MediaPlayer,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/first_screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="italic"
android:text="@string/first_screen"
/>
<EditText
android:id="@+id/first_screen_EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="@string/first_screen_edittext"
android:hint="@string/first_screen_edittext"
android:singleLine="false"
android:inputType="text"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Button1"
android:text="@string/button1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Button2"
android:text="@string/button2" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button3" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button4"
android:onClick="alert_Dialog"/>
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button5" />
<Button
android:id="@+id/button6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button6" />
<Button
android:id="@+id/button7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="alarm_sound"
android:text="@string/button7" />
<Button
android:id="@+id/button8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="on_Long_Click"
android:longClickable="true"
android:focusable="true"
android:text="@string/button8" />
<ImageView
android:id="@+id/first_screen_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="50dp"
android:src="@mipmap/incheon_national_university"
android:adjustViewBounds="true"
android:layout_marginStart="50dp"
android:contentDescription="@mipmap/incheon_national_university" />
</LinearLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/first_screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="italic"
android:text="@string/first_screen"
/>
<EditText
android:id="@+id/first_screen_EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="@string/first_screen_edittext"
android:hint="@string/first_screen_edittext"
android:singleLine="false"
android:inputType="text"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Button1"
android:text="@string/button1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Button2"
android:text="@string/button2" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button3" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button4"
android:onClick="alert_Dialog"/>
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button5" />
<Button
android:id="@+id/button6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button6" />
<Button
android:id="@+id/button7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="alarm_sound"
android:text="@string/button7" />
<Button
android:id="@+id/button8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="on_Long_Click"
android:longClickable="true"
android:focusable="true"
android:text="@string/button8" />
<ImageView
android:id="@+id/first_screen_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="50dp"
android:src="@mipmap/incheon_national_university"
android:adjustViewBounds="true"
android:layout_marginStart="50dp"
android:contentDescription="@mipmap/incheon_national_university" />
</LinearLayout>
</LinearLayout>
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState){ /* 가장 먼저 실행되는 함수*/
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView title = (TextView) findViewById(R.id.first_screen);
title.setText("R.string.Hello_INU");
mp=MediaPlayer.create(this, R.raw.alarm_sound);
}