我是Android的新手,特别是片段。 我正在尝试用2个活动创建一个简单的应用程序。第一个(MainActivity)发送一条从EditText(fragment_main_activity)获取的消息,并通过Intent将其带到第二个(SecondActivity),并使用textView(fragment_second_activity)将其取消。
我从整个网站的其他问题和建议中尝试了一些新的解决方案。我明白这个问题是看不见的。
我尝试删除textView并使用简单的Toast取消消息。它运行得很好。这是我在secondActivity.java中的onCreate方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
setContentView(R.layout.activity_second);
Intent intentMessaggio = getIntent();
String messaggioRicevuto = intentMessaggio.getStringExtra(getPackageName().concat(MainActivity.KEY_INTENT_MESSAGGIO));
Toast.makeText(getApplicationContext(), messaggioRicevuto, Toast.LENGTH_LONG).show();
// TextView textView1 = (TextView)findViewById(R.id.textView1);
// textView1.append(messaggioRicevuto);
}
这是Logcat:
05-07 21:37:44.740: E/AndroidRuntime(971): FATAL EXCEPTION: main
05-07 21:37:44.740: E/AndroidRuntime(971): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app_01/com.example.app_01.SecondActivity}: java.lang.NullPointerException
05-07 21:37:44.740: E/AndroidRuntime(971): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
05-07 21:37:44.740: E/AndroidRuntime(971): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
05-07 21:37:44.740: E/AndroidRuntime(971): at android.app.ActivityThread.access$700(ActivityThread.java:159)
05-07 21:37:44.740: E/AndroidRuntime(971): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
05-07 21:37:44.740: E/AndroidRuntime(971): at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 21:37:44.740: E/AndroidRuntime(971): at android.os.Looper.loop(Looper.java:176)
05-07 21:37:44.740: E/AndroidRuntime(971): at android.app.ActivityThread.main(ActivityThread.java:5419)
05-07 21:37:44.740: E/AndroidRuntime(971): at java.lang.reflect.Method.invokeNative(Native Method)
05-07 21:37:44.740: E/AndroidRuntime(971): at java.lang.reflect.Method.invoke(Method.java:525)
05-07 21:37:44.740: E/AndroidRuntime(971): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
05-07 21:37:44.740: E/AndroidRuntime(971): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
05-07 21:37:44.740: E/AndroidRuntime(971): at dalvik.system.NativeStart.main(Native Method)
05-07 21:37:44.740: E/AndroidRuntime(971): Caused by: java.lang.NullPointerException
05-07 21:37:44.740: E/AndroidRuntime(971): at com.example.app_01.SecondActivity.onCreate(SecondActivity.java:39)
05-07 21:37:44.740: E/AndroidRuntime(971): at android.app.Activity.performCreate(Activity.java:5372)
05-07 21:37:44.740: E/AndroidRuntime(971): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
05-07 21:37:44.740: E/AndroidRuntime(971): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
05-07 21:37:44.740: E/AndroidRuntime(971): ... 11 more
我尝试在secondActivity中第39行更改某些内容,但我无法解决问题。
这是我的MainActivity:
package com.example.app_01;
import android.app.Activity;
import android.app.ActionBar;
import android.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.webkit.WebView.FindListener;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class MainActivity extends Activity {
final static String KEY_INTENT_MESSAGGIO=".KEY_INTENT_MESSAGGIO";
public void inviaMessaggio(View view){
Intent intentMessaggio = new Intent(this,SecondActivity.class);
EditText editText1 = (EditText)findViewById(R.id.editText1);
String messaggioRicevuto = editText1.getText().toString();
intentMessaggio.putExtra(getPackageName().concat(KEY_INTENT_MESSAGGIO),messaggioRicevuto);
startActivity(intentMessaggio);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().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_main布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.app_01.MainActivity"
tools:ignore="MergeRootFrame" />
这是我的fragment_main_activity:
<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"
tools:context="com.example.app_01.MainActivity$PlaceholderFragment" >
<EditText
android:id="@+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/editText_inserisciIlMessaggio"
android:inputType="text">
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_invia"
android:onClick="inviaMessaggio"/>
这是我的第二个活动:
package com.example.app_01;
import android.app.Activity;
import android.app.ActionBar;
import android.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.widget.Toast;
import android.os.Build;
import android.provider.DocumentsContract.Root;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
setContentView(R.layout.activity_second);
Intent intentMessaggio = getIntent();
String messaggioRicevuto = intentMessaggio.getStringExtra(getPackageName().concat(MainActivity.KEY_INTENT_MESSAGGIO));
Toast.makeText(getApplicationContext(), messaggioRicevuto, Toast.LENGTH_LONG).show();
TextView textView1 = (TextView)findViewById(R.id.textView1);
textView1.append(messaggioRicevuto);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, 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_second,
container, false);
return rootView;
}
}
}
这是我的activity_second.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.app_01.SecondActivity"
tools:ignore="MergeRootFrame" />
这是我的fragment_second_aactivity.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.app_01.SecondActivity$PlaceholderFragment" >
<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="104dp"
android:text="@string/editText_inserisciIlMessaggio"/>
这是我的R.java:
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.example.app_01;
public final class R {
public static final class attr {
}
public static final class dimen {
/** Default screen margins, per the Android Design guidelines.
Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively).
*/
public static final int activity_horizontal_margin=0x7f040000;
public static final int activity_vertical_margin=0x7f040001;
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int action_settings=0x7f080004;
public static final int button1=0x7f080002;
public static final int container=0x7f080000;
public static final int editText1=0x7f080001;
public static final int textView1=0x7f080003;
}
public static final class layout {
public static final int activity_main=0x7f030000;
public static final int activity_second=0x7f030001;
public static final int fragment_main=0x7f030002;
public static final int fragment_second=0x7f030003;
}
public static final class menu {
public static final int main=0x7f070000;
public static final int second=0x7f070001;
}
public static final class string {
public static final int action_settings=0x7f050001;
public static final int app_name=0x7f050000;
public static final int button_invia=0x7f050003;
public static final int editText_inserisciIlMessaggio=0x7f050004;
public static final int title_activity_second=0x7f050002;
}
public static final class style {
/**
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
API 11 theme customizations can go here.
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
API 14 theme customizations can go here.
*/
public static final int AppBaseTheme=0x7f060000;
/** Application theme.
All customizations that are NOT specific to a particular API-level can go here.
*/
public static final int AppTheme=0x7f060001;
}
}
有我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app_01"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
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.app_01.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>
<activity
android:name="com.example.app_01.SecondActivity"
android:label="@string/title_activity_second" >
</activity>
</application>
</manifest>
感谢您的帮助。我不打算构建应用程序,但我有兴趣解决这个问题。
答案 0 :(得分:0)
导致空指针的行就是这一行:
textView1.append(messaggioRicevuto);
所以,textView1
是null
您的问题是您的活动布局不包含对R.id.textView1
的引用,而是在您的片段中引用了此textView,因此:如果您要使用它,请将代码移动到您的片段类或移动textView到您的活动布局(activity_second.xml
)。