Vote count:
0
Every time I click on the Start Button in my Android App, I consistently get a force close. There are zero errors in my code, so I'm confused as to why this is happening.
fragment_main_menu.xml:
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
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://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
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://ift.tt/nIICcg"
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
Also, let me know if you think I forgot to add in other code for you to view. Thanks so much for your help!
asked 44 secs ago
Aucun commentaire:
Enregistrer un commentaire