当我运行我的应用程序模拟器时,只会说“不幸的是,myapp 已停止”。logcat 抛出错误。我的 logcat 错误
04-21 15:24:13.661: I/Process(25846): Sending signal. PID: 25846 SIG: 9
04-21 15:24:34.572: D/AndroidRuntime(25911): Shutting down VM
04-21 15:24:34.572: W/dalvikvm(25911): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-21 15:24:34.592: E/AndroidRuntime(25911): FATAL EXCEPTION: main
04-21 15:24:34.592: E/AndroidRuntime(25911): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.loga/com.example.loga.MainActivity}: java.lang.NullPointerException
04-21 15:24:34.592: E/AndroidRuntime(25911): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-21 15:24:34.592: E/AndroidRuntime(25911): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-21 15:24:34.592: E/AndroidRuntime(25911): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-21 15:24:34.592: E/AndroidRuntime(25911): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-21 15:24:34.592: E/AndroidRuntime(25911): at android.os.Handler.dispatchMessage(Handler.java:99)
04-21 15:24:34.592: E/AndroidRuntime(25911): at android.os.Looper.loop(Looper.java:137)
04-21 15:24:34.592: E/AndroidRuntime(25911): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-21 15:24:34.592: E/AndroidRuntime(25911): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 15:24:34.592: E/AndroidRuntime(25911): at java.lang.reflect.Method.invoke(Method.java:511)
04-21 15:24:34.592: E/AndroidRuntime(25911): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-21 15:24:34.592: E/AndroidRuntime(25911): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-21 15:24:34.592: E/AndroidRuntime(25911): at dalvik.system.NativeStart.main(Native Method)
04-21 15:24:34.592: E/AndroidRuntime(25911): Caused by: java.lang.NullPointerException
04-21 15:24:34.592: E/AndroidRuntime(25911): at com.example.loga.MainActivity.onCreate(MainActivity.java:22)
04-21 15:24:34.592: E/AndroidRuntime(25911): at android.app.Activity.performCreate(Activity.java:5104)
04-21 15:24:34.592: E/AndroidRuntime(25911): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-21 15:24:34.592: E/AndroidRuntime(25911): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-21 15:24:34.592: E/AndroidRuntime(25911): ... 11 more
04-21 15:24:37.425: I/Process(25911): Sending signal. PID: 25911 SIG: 9
04-21 15:24:42.581: D/AndroidRuntime(25927): Shutting down VM
04-21 15:24:42.581: W/dalvikvm(25927): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-21 15:24:42.602: E/AndroidRuntime(25927): FATAL EXCEPTION: main
04-21 15:24:42.602: E/AndroidRuntime(25927): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.loga/com.example.loga.MainActivity}: java.lang.NullPointerException
04-21 15:24:42.602: E/AndroidRuntime(25927): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-21 15:24:42.602: E/AndroidRuntime(25927): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-21 15:24:42.602: E/AndroidRuntime(25927): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-21 15:24:42.602: E/AndroidRuntime(25927): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-21 15:24:42.602: E/AndroidRuntime(25927): at android.os.Handler.dispatchMessage(Handler.java:99)
04-21 15:24:42.602: E/AndroidRuntime(25927): at android.os.Looper.loop(Looper.java:137)
04-21 15:24:42.602: E/AndroidRuntime(25927): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-21 15:24:42.602: E/AndroidRuntime(25927): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 15:24:42.602: E/AndroidRuntime(25927): at java.lang.reflect.Method.invoke(Method.java:511)
04-21 15:24:42.602: E/AndroidRuntime(25927): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-21 15:24:42.602: E/AndroidRuntime(25927): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-21 15:24:42.602: E/AndroidRuntime(25927): at dalvik.system.NativeStart.main(Native Method)
04-21 15:24:42.602: E/AndroidRuntime(25927): Caused by: java.lang.NullPointerException
04-21 15:24:42.602: E/AndroidRuntime(25927): at com.example.loga.MainActivity.onCreate(MainActivity.java:22)
04-21 15:24:42.602: E/AndroidRuntime(25927): at android.app.Activity.performCreate(Activity.java:5104)
04-21 15:24:42.602: E/AndroidRuntime(25927): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-21 15:24:42.602: E/AndroidRuntime(25927): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-21 15:24:42.602: E/AndroidRuntime(25927): ... 11 more
04-21 15:24:46.051: I/Process(25927): Sending signal. PID: 25927 SIG: 9
04-21 15:28:28.302: E/Trace(26031): error opening trace file: No such file or directory (2)
04-21 15:28:29.192: D/AndroidRuntime(26031): Shutting down VM
04-21 15:28:29.192: W/dalvikvm(26031): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-21 15:28:29.202: E/AndroidRuntime(26031): FATAL EXCEPTION: main
04-21 15:28:29.202: E/AndroidRuntime(26031): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.loga/com.example.loga.MainActivity}: java.lang.NullPointerException
04-21 15:28:29.202: E/AndroidRuntime(26031): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-21 15:28:29.202: E/AndroidRuntime(26031): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-21 15:28:29.202: E/AndroidRuntime(26031): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-21 15:28:29.202: E/AndroidRuntime(26031): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-21 15:28:29.202: E/AndroidRuntime(26031): at android.os.Handler.dispatchMessage(Handler.java:99)
04-21 15:28:29.202: E/AndroidRuntime(26031): at android.os.Looper.loop(Looper.java:137)
04-21 15:28:29.202: E/AndroidRuntime(26031): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-21 15:28:29.202: E/AndroidRuntime(26031): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 15:28:29.202: E/AndroidRuntime(26031): at java.lang.reflect.Method.invoke(Method.java:511)
04-21 15:28:29.202: E/AndroidRuntime(26031): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-21 15:28:29.202: E/AndroidRuntime(26031): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-21 15:28:29.202: E/AndroidRuntime(26031): at dalvik.system.NativeStart.main(Native Method)
04-21 15:28:29.202: E/AndroidRuntime(26031): Caused by: java.lang.NullPointerException
04-21 15:28:29.202: E/AndroidRuntime(26031): at com.example.loga.MainActivity.onCreate(MainActivity.java:22)
04-21 15:28:29.202: E/AndroidRuntime(26031): at android.app.Activity.performCreate(Activity.java:5104)
04-21 15:28:29.202: E/AndroidRuntime(26031): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-21 15:28:29.202: E/AndroidRuntime(26031): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-21 15:28:29.202: E/AndroidRuntime(26031): ... 11 more
我不知道实际上哪里出错了,但编码没有错误。我的 .java 文件是
package com.example.loga;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
public class Login extends Activity {
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText inputpw;
private static String url_logincheck = "http://10.0.2.2/android_connect/logincheck.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Edit Text
inputpw = (EditText) findViewById(R.id.editText1);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
// creating new product in background thread
new check().execute();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
class check extends AsyncTask<String, String,String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Validating....");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
String pw = inputpw.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("pw",pw));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_logincheck,
"POST", params);
// check log cat for response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(Login.this).create();
alertDialog.setTitle("password verification..");
alertDialog.setMessage("success.!!");
alertDialog.show();
finish();
} else {
// failed to create product
}
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
}
我的activity_login.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=".Login" >
<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="31dp"
android:text="@string/password"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="136dp"
android:text="@string/click" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="36dp"
android:ems="10"
android:inputType="textPassword" />
</RelativeLayout>
我的 MainActivity.java 文件是:
package com.example.loga;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Context;
import android.content.Intent;
public class MainActivity extends Activity implements View.OnClickListener {
Button button1,button2,button3,button4;
final Context context = this;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3= (Button) findViewById(R.id.button3);
button4= (Button) findViewById(R.id.button4);
button1.setOnClickListener((OnClickListener) this);
button2.setOnClickListener((OnClickListener) this);
button3.setOnClickListener((OnClickListener) this);
button4.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
// do stuff
// TODO Auto-generated method stub
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, MainActivity1.class);
startActivity(intent);
}
});
break;
case R.id.button2:
// do stuff
// TODO Auto-generated method stub
button2= (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, Login.class);
startActivity(intent);
}
});
break;
case R.id.button3:
button3= (Button) findViewById(R.id.button3);
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, Recovery.class);
startActivity(intent);
}
});
break;
case R.id.button4:
button4= (Button) findViewById(R.id.button4);
button4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
finish();
System.exit(0);
}
});
}
}
@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;
}
}
编码有什么问题?帮助我。谢谢:)