这是一个我点击按钮时调用的活动,名称和电子邮件到达这个新活动,唯一的问题是当我尝试设置TextView的文本时我得到一个NPE。
package marcus.descubra_se;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class ResultadoActivity extends ActionBarActivity {
static TextView tvnome;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent it = getIntent();
String nome = "";
String email = "";
if(it != null){
Bundle params = it.getExtras();
if(params != null){
nome = params.getString("nome");
email = params.getString("email");
}
}
Toast.makeText(this, nome, Toast.LENGTH_LONG).show();
Toast.makeText(this, email, Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_resultado);
tvnome = (TextView)findViewById(R.id.textView1);
tvnome.setText(nome);
// Define que o conteúdo exibido pela tela é o campo que
// irá exibir o texto.
//tvnome.setText(nome);
/* TextView tvemail = (TextView) findViewById(R.id.textView5);
tvemail.setText(email);
*/ setContentView(R.layout.activity_resultado);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
public void voltar(View v){
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.resultado, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_resultado,
container, false);
return rootView;
}
}
}
这是xml文件:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="34dp"
android:onClick="voltar"
android:text="@string/voltar" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="110dp"
android:text="@string/abc_action_mode_done"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
这是logcat:
07-01 11:25:52.991: D/dalvikvm(1412): GC_FOR_ALLOC freed 80K, 8% free 2926K/3164K, paused 54ms, total 59ms
07-01 11:25:53.130: D/gralloc_goldfish(1412): Emulator without GPU emulation detected.
07-01 11:26:43.180: D/AndroidRuntime(1412): Shutting down VM
07-01 11:26:43.180: W/dalvikvm(1412): threadid=1: thread exiting with uncaught exception (group=0x41465700)
07-01 11:26:43.250: E/AndroidRuntime(1412): FATAL EXCEPTION: main
07-01 11:26:43.250: E/AndroidRuntime(1412): java.lang.RuntimeException: Unable to start activity ComponentInfo{marcus.descubra_se/marcus.descubra_se.ResultadoActivity}: java.lang.NullPointerException
07-01 11:26:43.250: E/AndroidRuntime(1412): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
07-01 11:26:43.250: E/AndroidRuntime(1412): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
07-01 11:26:43.250: E/AndroidRuntime(1412): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-01 11:26:43.250: E/AndroidRuntime(1412): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
07-01 11:26:43.250: E/AndroidRuntime(1412): at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 11:26:43.250: E/AndroidRuntime(1412): at android.os.Looper.loop(Looper.java:137)
07-01 11:26:43.250: E/AndroidRuntime(1412): at android.app.ActivityThread.main(ActivityThread.java:5103)
07-01 11:26:43.250: E/AndroidRuntime(1412): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 11:26:43.250: E/AndroidRuntime(1412): at java.lang.reflect.Method.invoke(Method.java:525)
07-01 11:26:43.250: E/AndroidRuntime(1412): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-01 11:26:43.250: E/AndroidRuntime(1412): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-01 11:26:43.250: E/AndroidRuntime(1412): at dalvik.system.NativeStart.main(Native Method)
07-01 11:26:43.250: E/AndroidRuntime(1412): Caused by: java.lang.NullPointerException
07-01 11:26:43.250: E/AndroidRuntime(1412): at marcus.descubra_se.ResultadoActivity.onCreate(ResultadoActivity.java:36)
07-01 11:26:43.250: E/AndroidRuntime(1412): at android.app.Activity.performCreate(Activity.java:5133)
07-01 11:26:43.250: E/AndroidRuntime(1412): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-01 11:26:43.250: E/AndroidRuntime(1412): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
07-01 11:26:43.250: E/AndroidRuntime(1412): ... 11 more
希望有人能帮助我!提前谢谢!