每次我点击Android应用程序中的“开始”按钮时,我都会一直关闭力量。我的代码中没有任何错误,所以我很困惑为什么会发生这种情况。
fragment_main_menu.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.app.whosesloganisthat.MainMenu$PlaceholderFragment" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/buttonStart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="37dp"
android:text="@string/start"
android:onClick="sendMessage" />
</RelativeLayout>
fragment_easy_level_info.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.app.whosesloganisthat.EasyLevelInfo$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
MainMenu.java:
package com.app.whosesloganisthat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
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.EditText;
import android.os.Build;
public class MainMenu extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.app.whosesloganisthat.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
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, 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);
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, EasyLevelInfo.class);
EditText editText = (EditText) findViewById(R.id.container);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
// Do something in response to button
}
/**
* 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_menu,
container, false);
return rootView;
}
}
}
EasyLevelInfo.java:
package com.app.whosesloganisthat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
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.os.Build;
public class EasyLevelInfo extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_easy_level_info);
Intent intent = getIntent();
String message = intent.getStringExtra(MainMenu.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);
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.easy_level_info, 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_easy_level_info,
container, false);
return rootView;
}
}
}
的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.whosesloganisthat"
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.app.whosesloganisthat.MainMenu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.app.whosesloganisthat.EasyLevelInfo"
android:label="@string/title_activity_easy_level_info" >
</activity>
<activity
android:name=".ToActivity"
android:label="@string/app_name">
</activity>
</application>
</manifest>
logcat的:
06-27 18:31:12.307: E/AndroidRuntime(16299): FATAL EXCEPTION: main
06-27 18:31:12.307: E/AndroidRuntime(16299): Process: com.app.whosesloganisthat, PID: 16299
06-27 18:31:12.307: E/AndroidRuntime(16299): java.lang.IllegalStateException: Could not execute method of the activity
06-27 18:31:12.307: E/AndroidRuntime(16299): at android.view.View$1.onClick(View.java)
06-27 18:31:12.307: E/AndroidRuntime(16299): at android.view.View.performClick(View.java)
06-27 18:31:12.307: E/AndroidRuntime(16299): at android.view.View$PerformClick.run(View.java)
06-27 18:31:12.307: E/AndroidRuntime(16299): at android.os.Handler.handleCallback(Handler.java)
06-27 18:31:12.307: E/AndroidRuntime(16299): at android.os.Handler.dispatchMessage(Handler.java)
06-27 18:31:12.307: E/AndroidRuntime(16299): at android.os.Looper.loop(Looper.java)
06-27 18:31:12.307: E/AndroidRuntime(16299): at android.app.ActivityThread.main(ActivityThread.java)
06-27 18:31:12.307: E/AndroidRuntime(16299): at java.lang.reflect.Method.invokeNative(Native Method)
06-27 18:31:12.307: E/AndroidRuntime(16299): at java.lang.reflect.Method.invoke(Method.java:515)
06-27 18:31:12.307: E/AndroidRuntime(16299): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
06-27 18:31:12.307: E/AndroidRuntime(16299): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
06-27 18:31:12.307: E/AndroidRuntime(16299): at dalvik.system.NativeStart.main(Native Method)
06-27 18:31:12.307: E/AndroidRuntime(16299): Caused by: java.lang.reflect.InvocationTargetException
06-27 18:31:12.307: E/AndroidRuntime(16299): at java.lang.reflect.Method.invokeNative(Native Method)
06-27 18:31:12.307: E/AndroidRuntime(16299): at java.lang.reflect.Method.invoke(Method.java:515)
06-27 18:31:12.307: E/AndroidRuntime(16299): ... 12 more
06-27 18:31:12.307: E/AndroidRuntime(16299): Caused by: java.lang.NullPointerException
06-27 18:31:12.307: E/AndroidRuntime(16299): at com.app.whosesloganisthat.MainMenu.sendMessage(MainMenu.java:51)
06-27 18:31:12.307: E/AndroidRuntime(16299): ... 14 more
另外,如果您认为我忘记添加其他代码供您查看,请与我们联系。 非常感谢你的帮助!
答案 0 :(得分:0)
试试这个..
从MainMenu
和EasyLevelInfo
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
MainMenu.java
更改此
setContentView(R.layout.activity_main_menu);
到
setContentView(R.layout.fragment_main_menu);
因为Button
onclick android:onClick="sendMessage"
仅适用于Activity
然后删除
setContentView(R.layout.activity_easy_level_info);
来自EasyLevelInfo.java
,因为您以编程方式将textview设置为查看,因此不需要setContentView(textView);
修改强>
<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.app.whosesloganisthat.MainMenu$PlaceholderFragment" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<EditText
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="55dp" />
<Button
android:id="@+id/buttonStart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="37dp"
android:text="@string/start"
android:onClick="sendMessage" />
</RelativeLayout>
答案 1 :(得分:0)
你正在第51行的sendMessage()
方法中抛出nullpointerexception,我假设它是以下行:
EditText editText = (EditText) findViewById(R.id.container);
我认为可能发生的情况是,当您点按开始按钮时,它会查找editText
带有ID“容器”,但找不到它。确保您的应用中确实有一个editText
ID为“容器”。