当我运行我的应用程序时出现此错误,不幸的是,“AppNameHere”已停止工作...
这是错误日志和文件。
05-23 18:03:23.537: D/ AndroidRuntime (1289): Shutting down VM
05-23 18:03:23.537: W/dalvikvm(1289): threadid=1: thread exiting with uncaught exception (group=0xb3a77ba8)
05-23 18:03:23.557: E/AndroidRuntime(1289): FATAL EXCEPTION: main
05-23 18:03:23.557: E/AndroidRuntime(1289): Process: com.example.holausuario, PID: 1289
05-23 18:03:23.557: E/AndroidRuntime(1289): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.holausuario/com.example.holausuario.MainActivity}: java.lang.NullPointerException
05-23 18:03:23.557: E/AndroidRuntime(1289): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-23 18:03:23.557: E/AndroidRuntime(1289): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-23 18:03:23.557: E/AndroidRuntime(1289): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-23 18:03:23.557: E/AndroidRuntime(1289): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-23 18:03:23.557: E/AndroidRuntime(1289): at android.os.Handler.dispatchMessage(Handler.java:102)
05-23 18:03:23.557: E/AndroidRuntime(1289): at android.os.Looper.loop(Looper.java:136)
05-23 18:03:23.557: E/AndroidRuntime(1289): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-23 18:03:23.557: E/AndroidRuntime(1289): at java.lang.reflect.Method.invokeNative(Native Method)
05-23 18:03:23.557: E/AndroidRuntime(1289): at java.lang.reflect.Method.invoke(Method.java:515)
05-23 18:03:23.557: E/AndroidRuntime(1289): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-23 18:03:23.557: E/AndroidRuntime(1289): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-23 18:03:23.557: E/AndroidRuntime(1289): at dalvik.system.NativeStart.main(Native Method)
05-23 18:03:23.557: E/AndroidRuntime(1289): Caused by: java.lang.NullPointerException
05-23 18:03:23.557: E/AndroidRuntime(1289): at com.example.holausuario.MainActivity.onCreate(MainActivity.java:34)
05-23 18:03:23.557: E/AndroidRuntime(1289): at android.app.Activity.performCreate(Activity.java:5231)
05-23 18:03:23.557: E/AndroidRuntime(1289): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-23 18:03:23.557: E/AndroidRuntime(1289): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-23 18:03:23.557: E/AndroidRuntime(1289): ... 11 more
05-23 18:06:35.097: I/Process(1289): Sending signal. PID: 1289 SIG: 9
manifiest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.holausuario"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
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.holausuario.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>
</application>
</manifest>
fragment_main.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"
tools:context="com.example.holausuario.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/LblNombre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Escribe tu nombre:" />
<Button
android:id="@+id/BtnHola"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/TxtNombre"
android:layout_below="@+id/TxtNombre"
android:layout_marginTop="18dp"
android:text="Aceptar" />
<EditText
android:id="@+id/TxtNombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/LblNombre"
android:layout_below="@+id/LblNombre"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="text" />
</RelativeLayout>
MainActivity.java
package com.example.holausuario;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
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.os.Build;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// OBTENGO LA REFERENCIA DE LOS OBJETOS CREADOS EN EL FORM DEL MAIN PRINCIPAL
final Button btnHola = (Button)findViewById(R.id.BtnHola);
final EditText txtNombre = (EditText)findViewById(R.id.TxtNombre);
//-----------------------------------------------------------------------------
btnHola.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Creamos el Intent, el cual se comunica entre distintas aplicacion y componentes.
Intent intent =
new Intent(MainActivity.this, FrmSaludo.class);
//Creamos la información a pasar entre actividades en el objeto Bundle que contiene la estructura ( clave, valor ), en pocas palabras son las variables que puedes enviar de pagina a pagina
Bundle b = new Bundle();
b.putString("NOMBRE", txtNombre.getText().toString()); // añadimos el variable string al objeto, mediante la estrcutura ( clave, valor )
//Añadimos la información al intent, ya que el intent es el que te permite interactuar de pagina a pagina.
intent.putExtras(b);
//Iniciamos la nueva actividad y le pasamos el intent el cual nos muestra a la pantalla que mandamos, osea iniciamos el envio de pagina a pagina
startActivity(intent);
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@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;
}
@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_main, container, false);
return rootView;
}
}
}
activity_saludo.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" >
<TextView
android:id="@+id/TxtSaludo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
FrmSaludo.java
package com.example.holausuario;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class FrmSaludo extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_saludo); // aqui decimos que esta clase manipula los objetos del activity_saludo.xml
// OBTENGO LA REFERENCIA DE LOS OBJETOS CREADOS EN EL FORM DEL MAIN PRINCIPAL
TextView txtSaludo = (TextView)findViewById(R.id.TxtSaludo);
// ------------------------------------------------------------------------
//Recuperamos la información pasada en el intent
Bundle bundle = this.getIntent().getExtras();
//Construimos el mensaje a mostrar
txtSaludo.setText("Hola " + bundle.getString("NOMBRE"));
}
}
我只是学习在android中编程
有人请帮助我......谢谢
答案 0 :(得分:1)
你的按钮在你的fragment_main.xml文件中,但在你的mainactivity中,你设置了SessionContent(R.layout.activity_main),所以在你的mainActivity中,你不能通过findviewbyid来获取一个按钮对象,按钮是null,所以你的程序抛出一个NullpointException。