gpt4 book ai didi

java.lang.RuntimeException : Unable to start activity ComponentInfo{. ..} : java. lang.NullPointerExceptionAndroid

转载 作者:行者123 更新时间:2023-12-01 13:47:02 25 4
gpt4 key购买 nike

我是 Android 开发新手,我只是尝试创建一个简单的应用程序,该应用程序获取输入并根据输入在新 Activity 中返回结果。调用新 Activity 后,我收到 NullPointerException。帮助将不胜感激!提前致谢。

日志猫:

11-29 19:03:47.369: D/libEGL(26311): loaded /system/lib/egl/libEGL_adreno200.so
11-29 19:03:47.369: D/libEGL(26311): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
11-29 19:03:47.369: D/libEGL(26311): loaded /system/lib/egl/libGLESv2_adreno200.so
11-29 19:03:47.369: I/Adreno200-EGL(26311): <qeglDrvAPI_eglInitialize:265>: EGL 1.4
QUALCOMM Build: Iabe52cfaeae4c5fab1acacfe6f056ba15fa93274
11-29 19:03:47.409: D/OpenGLRenderer(26311): Enabling debug mode 0
11-29 19:03:48.430: E/SpannableStringBuilder(26311): SPAN_EXCLUSIVE_EXCLUSIVE spans
cannot have a zero length
11-29 19:03:48.430: E/SpannableStringBuilder(26311): SPAN_EXCLUSIVE_EXCLUSIVE spans
cannot have a zero length
11-29 19:03:48.450: E/SpannableStringBuilder(26311): SPAN_EXCLUSIVE_EXCLUSIVE spans
cannot have a zero length
11-29 19:03:48.450: E/SpannableStringBuilder(26311): SPAN_EXCLUSIVE_EXCLUSIVE spans
cannot have a zero length
11-29 19:03:51.693: W/IInputConnectionWrapper(26311): beginBatchEdit on inactive
InputConnection
11-29 19:03:51.693: W/IInputConnectionWrapper(26311): endBatchEdit on inactive
InputConnection
11-29 19:03:54.606: D/AndroidRuntime(26311): Shutting down VM
11-29 19:03:54.606: W/dalvikvm(26311): threadid=1: thread exiting with uncaught
exception (group=0x416697c0)
11-29 19:03:54.606: E/AndroidRuntime(26311): FATAL EXCEPTION: main
11-29 19:03:54.606: E/AndroidRuntime(26311): java.lang.RuntimeException: Unable to
start activity ComponentInfo{com.example.newbloodexam/
com.example.newbloodexam.DisplayResultsActivity}: java.lang.NullPointerException
11-29 19:03:54.606: E/AndroidRuntime(26311): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
11-29 19:03:54.606: E/AndroidRuntime(26311): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2262)
11-29 19:03:54.606: E/AndroidRuntime(26311): at
android.app.ActivityThread.access$600(ActivityThread.java:142)
11-29 19:03:54.606: E/AndroidRuntime(26311): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
11-29 19:03:54.606: E/AndroidRuntime(26311): at
android.os.Handler.dispatchMessage(Handler.java:99)
11-29 19:03:54.606: E/AndroidRuntime(26311): at
android.os.Looper.loop(Looper.java:137)
11-29 19:03:54.606: E/AndroidRuntime(26311): at
android.app.ActivityThread.main(ActivityThread.java:5104)
11-29 19:03:54.606: E/AndroidRuntime(26311): at
java.lang.reflect.Method.invokeNative(Native Method)
11-29 19:03:54.606: E/AndroidRuntime(26311): at
java.lang.reflect.Method.invoke(Method.java:525)
11-29 19:03:54.606: E/AndroidRuntime(26311): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-29 19:03:54.606: E/AndroidRuntime(26311): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-29 19:03:54.606: E/AndroidRuntime(26311): at
dalvik.system.NativeStart.main(Native Method)
11-29 19:03:54.606: E/AndroidRuntime(26311): Caused by: java.lang.NullPointerException
11-29 19:03:54.606: E/AndroidRuntime(26311): at
com.example.newbloodexam.DisplayResultsActivity.onCreate
(DisplayResultsActivity.java:36)
11-29 19:03:54.606: E/AndroidRuntime(26311): at
android.app.Activity.performCreate(Activity.java:5133)
11-29 19:03:54.606: E/AndroidRuntime(26311): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-29 19:03:54.606: E/AndroidRuntime(26311): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
11-29 19:03:54.606: E/AndroidRuntime(26311): ... 11 more
11-29 19:03:56.478: I/Process(26311): Sending signal. PID: 26311 SIG: 9

主要 Activity :

package com.example.newbloodexam;

import com.example.newbloodexam.DisplayResultsActivity;
import com.example.newbloodexam.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

public void CalcResult(View view)
{

String NameExtra = "NameStr"; String AgeExtra = "AgeInt"; String TestExtra = "TestInt";
Intent intent = new Intent(this, DisplayResultsActivity.class);
EditText NameEdit = (EditText) findViewById(R.id.EnterName);
EditText AgeEdit = (EditText) findViewById(R.id.EnterAge);
EditText TestEdit = (EditText) findViewById(R.id.EnterTest);
String NameStr = NameEdit.getText().toString();
int AgeInt = Integer.parseInt(AgeEdit.getText().toString());
int TestInt = Integer.parseInt(TestEdit.getText().toString());
Bundle ExtraBundle = new Bundle();
ExtraBundle.putString(NameExtra, NameStr);
ExtraBundle.putInt(AgeExtra, AgeInt);
ExtraBundle.putInt(TestExtra, TestInt);
intent.putExtras(ExtraBundle);
startActivity(intent);


}

}

显示结果 Activity :

package com.example.newbloodexam;

import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class DisplayResultsActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_display_results);
// Show the Up button in the action bar.
setupActionBar();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}

Intent intent = getIntent();
Bundle Extra = intent.getExtras();
String Name = Extra.getString("NameStr");
int Age = Extra.getInt("AgeInt");
int Test = Extra.getInt("TestInt");
String Result = Calculate(Name,Age,Test);
//TextView textView = new TextView(this);
//textView.setTextSize(40);
//textView.setText(Result);
//setContentView(textView);
TextView ResultVal = (TextView) findViewById(R.id.textView3);
ResultVal.setText(Result);
setContentView(ResultVal);

}

/**
* Set up the {@link android.app.ActionBar}.
*/
private void setupActionBar() {

getActionBar().setDisplayHomeAsUpEnabled(true);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_results, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-
vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}

public String Calculate(String getName, int getAge, int getTest)
{
String ResultText = "";
if(getAge < 18)
{
if(getTest > 0 && getTest <= 100)
{
ResultText = "Your ok.";
}
else if(getTest > 100)
{
ResultText = "wtf?";
}
}
else if(getAge < 18 && getAge <= 50)
{
if(getTest > 0 && getTest >= 500)
{
ResultText = "Your a healthy one";
}
else if(getTest > 500)
{
ResultText = "Your gonna die.";
}
}

return ResultText;
}

}

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newbloodexam"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="19" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.newbloodexam.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.newbloodexam.DisplayResultsActivity"
android:label="@string/title_activity_display_results" >
</activity>
</application>

</manifest>

最佳答案

看来问题出在:

TextView ResultVal = (TextView) findViewById(R.id.textView3);
ResultVal.setText(Result);
setContentView(ResultVal);

ResultVal 为空。
您不能在 setContentView 之前使用 findViewById

关于java.lang.RuntimeException : Unable to start activity ComponentInfo{. ..} : java. lang.NullPointerExceptionAndroid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20290776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com