我是一个新手,我正在学习制作Android应用程序。使用其中一个教程。事情进展顺利,但由于某些原因,我的应用程序在我每次启动时都会开始粉碎。
我真的很想在LogCat中看到原因,但我没有成功。如果有人告诉我发生了什么,我将非常感激。
这是LogCat:
08-30 19:13:21.955: W/dalvikvm(7272): threadid=1: thread exiting with uncaught exception (group=0x41869d58)
08-30 19:13:21.955: E/AndroidRuntime(7272): FATAL EXCEPTION: main
08-30 19:13:21.955: E/AndroidRuntime(7272): Process: alesto.androidinterview, PID: 7272
08-30 19:13:21.955: E/AndroidRuntime(7272): java.lang.RuntimeException: Unable to start activity ComponentInfo{alesto.androidinterview/alesto.androidinterview.SimpleQuestion}: java.lang.NullPointerException
08-30 19:13:21.955: E/AndroidRuntime(7272): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
08-30 19:13:21.955: E/AndroidRuntime(7272): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2259)
08-30 19:13:21.955: E/AndroidRuntime(7272): at android.app.ActivityThread.access$800(ActivityThread.java:149)
08-30 19:13:21.955: E/AndroidRuntime(7272): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
08-30 19:13:21.955: E/AndroidRuntime(7272): at android.os.Handler.dispatchMessage(Handler.java:102)
08-30 19:13:21.955: E/AndroidRuntime(7272): at android.os.Looper.loop(Looper.java:136)
08-30 19:13:21.955: E/AndroidRuntime(7272): at android.app.ActivityThread.main(ActivityThread.java:5110)
08-30 19:13:21.955: E/AndroidRuntime(7272): at java.lang.reflect.Method.invokeNative(Native Method)
08-30 19:13:21.955: E/AndroidRuntime(7272): at java.lang.reflect.Method.invoke(Method.java:515)
08-30 19:13:21.955: E/AndroidRuntime(7272): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
08-30 19:13:21.955: E/AndroidRuntime(7272): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-30 19:13:21.955: E/AndroidRuntime(7272): at dalvik.system.NativeStart.main(Native Method)
08-30 19:13:21.955: E/AndroidRuntime(7272): Caused by: java.lang.NullPointerException
08-30 19:13:21.955: E/AndroidRuntime(7272): at alesto.androidinterview.SimpleQuestion.onCreate(SimpleQuestion.java:56)
08-30 19:13:21.955: E/AndroidRuntime(7272): at android.app.Activity.performCreate(Activity.java:5231)
08-30 19:13:21.955: E/AndroidRuntime(7272): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-30 19:13:21.955: E/AndroidRuntime(7272): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
08-30 19:13:21.955: E/AndroidRuntime(7272): ... 11 more
08-30 19:13:21.965: V/ActivityThread(7272): SCHEDULE 102 PAUSE_ACTIVITY_FINISHING: 0 / android.os.BinderProxy@429157e0
08-30 19:13:22.535: V/ActivityThread(7272): SCHEDULE 109 DESTROY_ACTIVITY: 1 / android.os.BinderProxy@428cbe18
08-30 19:13:22.535: V/ActivityThread(7272): SCHEDULE 140 TRIM_MEMORY: 20 / null
08-30 19:13:22.535: V/ActivityThread(7272): SCHEDULE 109 DESTROY_ACTIVITY: 1 / android.os.BinderProxy@429157e0
以下是首页活动:
package alesto.androidinterview;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.app.ActionBar;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
public class FrontPage extends ActionBarActivity implements OnClickListener {
Button bsimple, btough, bseeapps, brateapp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.front_page);
//initializing buttons
bsimple = (Button) findViewById(R.id.bsq);
btough = (Button) findViewById(R.id.btq);
bseeapps = (Button) findViewById(R.id.bseeotherapps);
brateapp = (Button) findViewById(R.id.brateapp);
bsimple.setOnClickListener(this);
btough.setOnClickListener(this);
bseeapps.setOnClickListener(this);
brateapp.setOnClickListener(this);
//adding action bar
LinearLayout frontLL = (LinearLayout) findViewById(R.id.front_page_titlebar);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.front_page_title_bar);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bsq:
Intent i = new Intent(FrontPage.this, SimpleQuestion.class);
startActivity(i);
break;
case R.id.btq:
Intent j = new Intent(FrontPage.this, ToughQuestion.class);
startActivity(j);
break;
case R.id.bseeotherapps:
break;
case R.id.brateapp:
Uri uri1 = Uri.parse("market://details?id="+getPackageName());
Intent goToMarket1 = new Intent(Intent.ACTION_VIEW, uri1);
startActivity(goToMarket1);
break;
}
}
}
及其' XML文件:
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/bgfirst"
android:id="@+id/frontpagelayout" >
<Button
android:id="@+id/bsq"
android:layout_width="200dp"
android:layout_height="40dp"
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:background="@drawable/button_custom"
android:textColor="#050000"
android:text="@string/sq" />
<Button
android:id="@+id/btq"
android:layout_width="200dp"
android:layout_height="40dp"
android:textSize="20sp"
android:layout_below="@id/bsq"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:background="@drawable/button_custom"
android:textColor="#050000"
android:text="@string/tq" />
<Button
android:id="@+id/bseeotherapps"
android:layout_width="250dp"
android:layout_height="40dp"
android:textSize="20sp"
android:layout_below="@id/btq"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:background="@drawable/button_custom"
android:textColor="#050000"
android:text="@string/soa" />
<Button
android:id="@+id/brateapp"
android:layout_width="200dp"
android:layout_height="40dp"
android:textSize="20sp"
android:layout_below="@id/bseeotherapps"
android:layout_centerHorizontal="true"
android:layout_marginTop="49dp"
android:background="@drawable/button_custom"
android:textColor="#050000"
android:text="@string/ra" />
</RelativeLayout>
目的地活动:
package alesto.androidinterview;
import java.util.Locale;
import android.app.ActionBar;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class SimpleQuestion extends ActionBarActivity implements
OnClickListener {
TextView tvQuestion, tvAnswer, tvTotalLength, tvPresentIndex;
Button bleft, bright, bshow;
String[] simpleQuestions, simpleAnswers;
int index;
public static final String defaultAnswer = "Press \"A\" for the answer";
// variables and object of text-to-speech
TextToSpeech ttsobject;
int result;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.questions);
// initializiation of textviews and buttons
tvQuestion = (TextView) findViewById(R.id.tvquestion);
tvAnswer = (TextView) findViewById(R.id.tvanswer);
tvTotalLength = (TextView) findViewById(R.id.tvyy);
tvPresentIndex = (TextView) findViewById(R.id.tvxx);
bleft = (Button) findViewById(R.id.bleft);
bright = (Button) findViewById(R.id.bright);
bshow = (Button) findViewById(R.id.bshowanswer);
Button bspeak = (Button) findViewById(R.id.bspeak);
Button bstop = (Button) findViewById(R.id.bstop_mute);
// setting onclick listeners to 3 buttons and SPEAK/MUTE buttons
bleft.setOnClickListener(this);
bright.setOnClickListener(this);
bshow.setOnClickListener(this);
bspeak.setOnClickListener(this);
bstop.setOnClickListener(this);
// importing string arrays
simpleQuestions = getResources().getStringArray(R.array.simple_ques);
simpleAnswers = getResources().getStringArray(R.array.simple_ans);
// setting values to different variables
index = 0;
tvQuestion.setText(simpleQuestions[index]);
tvAnswer.setText(defaultAnswer);
tvPresentIndex.setText(String.valueOf(index + 1));
tvTotalLength.setText("/" + String.valueOf(simpleQuestions.length));
// adding action bar and customizing it
LinearLayout questionLL = (LinearLayout) findViewById(R.id.question_page_titlebar);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.questions_title_bar);
TextView category = (TextView) findViewById(R.id.tv_questions_titlebar);
category.setText("Simple questions");
// initialization of Text-to-Speech object
ttsobject = new TextToSpeech(SimpleQuestion.this,
new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
ttsobject.setLanguage(Locale.ENGLISH);
} else {
Toast.makeText(getApplicationContext(),
"Feature is not supported in your device",
Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bleft:
bshow.setActivated(false);
tvAnswer.setText(defaultAnswer);
index--;
if (index == -1) {
index = simpleQuestions.length - 1;
}
tvQuestion.setText(simpleQuestions[index]);
tvPresentIndex.setText(String.valueOf(index + 1));
break;
case R.id.bright:
bshow.setActivated(false);
tvAnswer.setText(defaultAnswer);
index++;
if (index == simpleQuestions.length) {
index = 0;
}
tvQuestion.setText(simpleQuestions[index]);
tvPresentIndex.setText(String.valueOf(index + 1));
break;
case R.id.bshowanswer:
tvAnswer.setText(simpleAnswers[index]);
bshow.setActivated(true);
break;
case R.id.bspeak:
if (result == TextToSpeech.LANG_NOT_SUPPORTED
|| result == TextToSpeech.LANG_MISSING_DATA) {
Toast.makeText(getApplicationContext(),
"Feature is not supported in your device",
Toast.LENGTH_SHORT).show();
} else {
if (!tvAnswer.getText().toString().equals(defaultAnswer)) {
ttsobject.speak(simpleAnswers[index],
TextToSpeech.QUEUE_FLUSH, null);
}
}
break;
case R.id.bstop_mute:
if (ttsobject != null) {
ttsobject.stop();
}
break;
}
}
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (ttsobject != null) {
ttsobject.stop();
ttsobject.shutdown();
}
}
}
目标 XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:gravity="center"
android:background="@drawable/bg_topbottom" >
<TextView
android:id="@+id/tvxx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tv_xx"
android:textColor="#000000" />
<TextView
android:id="@+id/tvyy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tv_yy"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="80"
android:background="@drawable/main_background" >
<TextView
android:id="@+id/tvquestion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="18dp"
android:text="@string/tv_question"
android:textColor="#000000" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tvanswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="@string/tv_answer"
android:textColor="#000000" />
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="12"
android:gravity="center_horizontal|center"
android:background="@drawable/bg_topbottom" >
<Button
android:id="@+id/bleft"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/left_button" />
<Button
android:id="@+id/bshowanswer"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/a_button" />
<Button
android:id="@+id/bright"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/right_button" />
</LinearLayout>
</LinearLayout>
在 Eclipse 中尝试清除功能。不幸的是没有帮助。
答案 0 :(得分:0)
底层错误在logcat中指定:
08-30 19:13:21.955:E / AndroidRuntime(7272):引起:java.lang.NullPointerException 08-30 19:13:21.955:E / AndroidRuntime(7272):在alesto.androidinterview.SimpleQuestion.onCreate(SimpleQuestion.java:56)
,第56行是:
bspeak.setOnClickListener(this);
所以bspeak
为空。 <{1}}由
bspeak
这意味着该视图不包含ID为Button bspeak = (Button) findViewById(R.id.bspeak);
的视图。您应该创建缺少的视图R.id.bspeak
或删除引用它的代码。
答案 1 :(得分:0)
嗨,这两个按钮bspeak
和bstop_mute
没有xlm,你得到java.lang.NullPointerException
android所有方式通过id引用。
Button bspeak = (Button) findViewById(R.id.bspeak);
Button bstop = (Button) findViewById(R.id.bstop_mute);
请为此ID创建一个按钮,然后您就可以了
答案 2 :(得分:0)
非常感谢!
实际上bspeak
已在单独的XML文件中定义,其中标题栏被描述为独立布局。
其他请求很容易找到这种观点。我假设在onCreate方法中定义Button对象是个坏主意。
Button bspeak = (Button) findViewById(R.id.bspeak);
Button bstop = (Button) findViewById(R.id.bstop_mute);
与其他按钮不同,Eclipse没有改变颜色并且没有使单词bspeak
和bstop
显示出来。分别OnClickListener
没有在下面的字符串中设置:
bspeak.setOnClickListener(this);
bstop.setOnClickListener(this);
但即便如此也没有成功。真正的问题是关于字符串
LinearLayout questionLL = (LinearLayout) findViewById(R.id.question_page_titlebar);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.questions_title_bar);
是在初始化按钮bspeak
和bstop
后编写的。上面这些字符串上升解决了问题。这有点奇怪。