我正在尝试创建一个应用。测试时,每当我尝试按下按钮时,手机上的应用程序都会显示,#34;不幸的是,[应用程序名称]已经停止。"
在logcat上,会显示一条消息:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.inspiron.firstapplication, PID: 6746
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.inspiron.firstapplication/com.example.inspiron.firstapplication.Text}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2321)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2507)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5692)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:68)
at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:190)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:172)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:512)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:184)
at com.example.inspiron.firstapplication.Text.<init>(Text.java:17)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2312)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2507)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5692)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
我的XML看起来像这样:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="English:"
android:id="@+id/LanguageTranslatedFrom"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:textSize="20dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/TextBar"
android:layout_below="@+id/LanguageTranslatedFrom"
android:layout_toEndOf="@+id/LanguageTranslatedFrom"
android:hint="PUT IN what you want to translate..." />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Spanish:"
android:id="@+id/LanguageTranslatedTo"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:textSize="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:id="@+id/ConfirmationButton"
android:layout_below="@+id/TextBar"
android:layout_alignParentEnd="true"
android:background="#61ff00" />
<ImageButton
android:layout_width="50dp"
android:layout_height="100dp"
android:id="@+id/TextIcon"
android:src="@drawable/keyboard"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/LanguageTranslatedTo"
android:layout_alignParentEnd="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ViaVoiceButton"
android:layout_below="@+id/LanguageTranslatedTo"
android:layout_alignParentStart="true"
android:src="@drawable/microphone"
android:layout_toStartOf="@+id/Result" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="What the output is..."
android:id="@+id/Result"
android:layout_below="@+id/TextIcon"
android:layout_toEndOf="@+id/LanguageTranslatedTo"
android:textSize="18dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Retry"
android:id="@+id/Retryit"
android:layout_below="@+id/ConfirmationButton"
android:layout_alignParentEnd="true"
android:background="#61ff00" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BACK"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_toStartOf="@+id/Result"
android:background="#ff0000" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/LanguagePicker"
android:layout_above="@+id/TextBar"
android:layout_toEndOf="@+id/Result" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/LanguageTranslatedTo"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView2"
android:layout_gravity="left|top" />
</FrameLayout>
最后,我的java看起来像这样:
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
public class Text extends AppCompatActivity {
final Context context = this;
Button button = (Button) findViewById(R.id.ConfirmationButton);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text_wall);
Spinner spinner = (Spinner) findViewById(R.id.LanguagePicker);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.language, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.alertdialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder.setView(promptsView);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
Spinner spinner = (Spinner) findViewById(R.id.LanguagePicker);
String text = spinner.getSelectedItem().toString();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
}
}
答案 0 :(得分:3)
在onCreate方法中移动按钮初始化。您无法在此方法之前执行此操作,因为视图未呈现且项目尚未就绪。 像下面这样更改java代码:
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
public class Text extends AppCompatActivity {
Context context;
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text_wall);
Spinner spinner = (Spinner) findViewById(R.id.LanguagePicker);
button = (Button) findViewById(R.id.ConfirmationButton);
context = this; // why this? why using context when u have this?
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.language, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.alertdialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder.setView(promptsView);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
Spinner spinner = (Spinner) findViewById(R.id.LanguagePicker);
String text = spinner.getSelectedItem().toString();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
}
}
在成瘾中检查您的活动是否存在于清单中。如果没有发布完整的清单请