好吧所以我正在制作一个配置文件应用程序,它正在制作一系列问题,并在最后将每个答案添加给它们并为其添加一个值,并为此提供文本结果我使用putExtra以便跟踪结果,但是当我尝试去结果活动它只是崩溃,所有活动都在清单中声明
这是第一个到目前为止一切顺利进行的活动:
package com.springair.perfiladorsam;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Donde extends Activity implements OnClickListener {
Button cama;
Button litera;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.donde);
setupVariables();
}
private void setupVariables() {
// TODO Auto-generated method stub
Button cama = (Button) findViewById(R.id.cama);
Button litera = (Button) findViewById(R.id.litera);
cama.setOnClickListener(this);
litera.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent resultado = new Intent ("com.springair.perfilador.RESULTADO");
Bundle extras = getIntent().getExtras();
int resulta = extras.getInt("StoredData");
switch(v.getId()){
case R.id.Delg:
resulta = resulta + 5;
resultado.putExtra("StoredData", (int)resulta);
startActivity(resultado);
break;
case R.id.Medio:
resulta = resulta + 3;
resultado.putExtra("StoredData", (int)resulta);
startActivity(resultado);
break;
}
}
}
然后是结果Activity:但它永远不会达到它:
package com.springair.perfiladorsam;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Resultado extends Activity implements OnClickListener{
Button reinicio;
TextView Firmeza;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setContentView(R.layout.resultado);
super.onCreate(savedInstanceState);
setupVariables();
Bundle extras = getIntent().getExtras();
int resulta = extras.getInt("StoredData");
if(resulta<5){
Firmeza.setText("Firme");
}
else if (resulta <10){
Firmeza.setText("Medio");
}
else{
Firmeza.setText("Suave");
}
}
private void setupVariables() {
// TODO Auto-generated method stub
reinicio = (Button)findViewById(R.id.Reinicio);
Firmeza = (TextView)findViewById(R.id.Nivel);
reinicio.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cheese = new Intent("com.springair.perfilador.MAINMENU");
startActivity(cheese);
}
}
¿我做错了什么?同样,所有内容都在清单中声明,名称是正确的,我已经检查了两次。
这是LogCat
01-31 23:16:46.684: E/AndroidRuntime(831): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException
01-31 23:16:46.684: E/AndroidRuntime(831): at android.app.LoadedApk.makeApplication(LoadedApk.java:504)
01-31 23:16:46.684: E/AndroidRuntime(831): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4364)
01-31 23:16:46.684: E/AndroidRuntime(831): at android.app.ActivityThread.access$1300(ActivityThread.java:141)
01-31 23:16:46.684: E/AndroidRuntime(831): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
01-31 23:16:46.684: E/AndroidRuntime(831): at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 23:16:46.684: E/AndroidRuntime(831): at android.os.Looper.loop(Looper.java:137)
01-31 23:16:46.684: E/AndroidRuntime(831): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-31 23:16:46.684: E/AndroidRuntime(831): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 23:16:46.684: E/AndroidRuntime(831): at java.lang.reflect.Method.invoke(Method.java:511)
01-31 23:16:46.684: E/AndroidRuntime(831): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-31 23:16:46.684: E/AndroidRuntime(831): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-31 23:16:46.684: E/AndroidRuntime(831): at dalvik.system.NativeStart.main(Native Method)
01-31 23:16:46.684: E/AndroidRuntime(831): Caused by: java.lang.NullPointerException
01-31 23:16:46.684: E/AndroidRuntime(831): at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:379)
01-31 23:16:46.684: E/AndroidRuntime(831): at android.app.LoadedApk.getClassLoader(LoadedApk.java:322)
01-31 23:16:46.684: E/AndroidRuntime(831): at android.app.LoadedApk.makeApplication(LoadedApk.java:496)
答案 0 :(得分:1)
错误发生在您的结果活动中。在onCreate方法中你有
...
// TODO Auto-generated method stub
setContentView(R.layout.resultado);
super.onCreate(savedInstanceState);
setupVariables();
...
您需要将呼叫转移到super.onCreate(savedInstanceState);
到方法的开头。当处理android生命周期中的重写方法时,一些方法将首先调用super方法,而最后调用其他方法。他们中的任何人都不会在两者之间被召唤。检查开发人员文档以确定将来何时调用超级用户。
答案 1 :(得分:0)
在Resultado中,您需要先致电super.onCreate(savedInstanceState)
,然后致电setContentView()
。