我按照官方android网站的说明创建我的第一个Android应用程序。当我单击按钮“发送”消息时,应用程序崩溃。
MainActivity:
package com.example.myfirstapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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;
}
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
第一个xml文件:
<LinearLayout 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"
android:orientation="horizontal"
tools:context="com.example.myfirstapp.MainActivity$PlaceholderFragment" >
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"/>
</LinearLayout>
DisplayMessageActivity:
package com.example.myfirstapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
@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);
}
}
我的logcat:
07-25 20:06:16.196: E/DatabaseUtils(819): Writing exception to parcel
07-25 20:06:16.196: E/DatabaseUtils(819): java.lang.SecurityException: Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
07-25 20:06:21.201: E/DatabaseUtils(819): Writing exception to parcel
07-25 20:06:21.201: E/DatabaseUtils(819): java.lang.SecurityException: Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
07-25 20:06:21.281: W/dalvikvm(15329): threadid=1: thread exiting with uncaught exception (group=0x41745da0)
07-25 20:06:21.281: E/AndroidRuntime(15329): FATAL EXCEPTION: main
07-25 20:06:21.281: E/AndroidRuntime(15329): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.NullPointerException
07-25 20:06:21.281: E/AndroidRuntime(15329): Caused by: java.lang.NullPointerException
07-25 20:06:46.296: V/AwesomePlayer(284): checkOffloadExceptions is true
07-25 20:06:46.316: V/AwesomePlayer(284): checkOffloadExceptions is true
07-25 20:06:46.336: V/AwesomePlayer(284): checkOffloadExceptions is true
07-25 20:06:46.996: E/ShotSingle(284): <=== Exception : metadata is NULL ===>
07-25 20:06:47.066: E/ShotSingle(284): <=== Exception : metadata is NULL ===>
07-25 20:06:47.136: E/ShotSingle(284): <=== Exception : metadata is NULL ===>
07-25 20:06:47.206: E/ShotSingle(284): <=== Exception : metadata is NULL ===>
07-25 20:06:47.277: E/ShotSingle(284): <=== Exception : metadata is NULL ===>
07-25 20:06:47.347: E/ShotSingle(284): <=== Exception : metadata is NULL ===>
07-25 20:06:47.417: E/ShotSingle(284): <=== Exception : metadata is NULL ===>
logcat update:
07-25 20:14:47.355: E/android.os.Debug(819): !@Dumpstate > sdumpstate -k -t -z -d -o /data/log/dumpstate_app_error
07-25 20:14:53.601: E/QSEECOMAPI:(16677): Error::Failed to open /dev/qseecom device
07-25 20:14:53.601: E/QSEECOMAPI:(16677): Error::Failed to open /dev/qseecom device
07-25 20:14:53.601: E/QSEECOMAPI:(16677): Error::Failed to open /dev/qseecom device
07-25 20:14:53.601: E/QSEECOMAPI:(16677): Error::Failed to open /dev/qseecom device
07-25 20:14:53.601: E/QSEECOMAPI:(16677): Error::Failed to open /dev/qseecom device
07-25 20:15:10.157: E/QCameraHWI_Parm(284): setPreviewFpsRange: error: FPS range (15, 30) not supported
答案 0 :(得分:2)
你是否在manifest.xml中声明了你的Activity?
它应该是这样的:
<activity android:name="YOURPATH.DisplayMessageActivity" />
它将被放置为.Activity2:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="your.package.name">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Activity1" android:label="@string/app_name"></activity>
<activity android:name=".Activity2"></activity>
</application>
<uses-sdk android:minSdkVersion="4" />
</manifest>