activity_main.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.liangjie.s01_e05_view.MainActivity"
tools:ignore="MergeRootFrame" />
fragment_main.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:orientation="vertical"
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.liangjie.s01_e05_view.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF0000"
android:text="0" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button"/>
</LinearLayout>
MainActivity.java:
package com.liangjie.s01_e05_view;
import android.app.Activity;
import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
private Button button;
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);
button = (Button)findViewById(R.id.button);
ButtonListener buttonListener = new ButtonListener();
button.setOnClickListener(buttonListener);
//textView.setText("Hello Jie");
//textView.setBackgroundColor(Color.BLUE);
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;
}
class ButtonListener implements OnClickListener {
@Override
public void onClick(View arg0) {
count ++;
textView.setTag(count + "");
}
}
@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;
}
}
}
LogCat日志:
01-25 00:41:11.993: D/AndroidRuntime(1522): Shutting down VM
01-25 00:41:11.993: W/dalvikvm(1522): threadid=1: thread exiting with uncaught exception (group=0xb3a3bba8)
01-25 00:41:12.013: E/AndroidRuntime(1522): FATAL EXCEPTION: main
01-25 00:41:12.013: E/AndroidRuntime(1522): Process: com.liangjie.s01_e05_view, PID: 1522
01-25 00:41:12.013: E/AndroidRuntime(1522): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.liangjie.s01_e05_view/com.liangjie.s01_e05_view.MainActivity}: java.lang.NullPointerException
01-25 00:41:12.013: E/AndroidRuntime(1522): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-25 00:41:12.013: E/AndroidRuntime(1522): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-25 00:41:12.013: E/AndroidRuntime(1522): at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-25 00:41:12.013: E/AndroidRuntime(1522): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-25 00:41:12.013: E/AndroidRuntime(1522): at android.os.Handler.dispatchMessage(Handler.java:102)
01-25 00:41:12.013: E/AndroidRuntime(1522): at android.os.Looper.loop(Looper.java:136)
01-25 00:41:12.013: E/AndroidRuntime(1522): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-25 00:41:12.013: E/AndroidRuntime(1522): at java.lang.reflect.Method.invokeNative(Native Method)
01-25 00:41:12.013: E/AndroidRuntime(1522): at java.lang.reflect.Method.invoke(Method.java:515)
01-25 00:41:12.013: E/AndroidRuntime(1522): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-25 00:41:12.013: E/AndroidRuntime(1522): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-25 00:41:12.013: E/AndroidRuntime(1522): at dalvik.system.NativeStart.main(Native Method)
01-25 00:41:12.013: E/AndroidRuntime(1522): Caused by: java.lang.NullPointerException
01-25 00:41:12.013: E/AndroidRuntime(1522): at com.liangjie.s01_e05_view.MainActivity.onCreate(MainActivity.java:30)
01-25 00:41:12.013: E/AndroidRuntime(1522): at android.app.Activity.performCreate(Activity.java:5231)
01-25 00:41:12.013: E/AndroidRuntime(1522): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-25 00:41:12.013: E/AndroidRuntime(1522): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-25 00:41:12.013: E/AndroidRuntime(1522): ... 11 more
01-25 00:41:13.983: I/Process(1522): Sending signal. PID: 1522 SIG: 9
求高手解答一下。。。
4 个解决方案
#1
看logcat的错误就很明白了。
你的setContentView加载的是R.layout.activity_main
但是你的button的实际位置是R.layout.fragment_main
所以你用findviewbyid获得的是null,最后运行报的是空指针
只要把第24行改为setContentView(R.layout.fragment_main);
你的setContentView加载的是R.layout.activity_main
但是你的button的实际位置是R.layout.fragment_main
所以你用findviewbyid获得的是null,最后运行报的是空指针
只要把第24行改为setContentView(R.layout.fragment_main);
#2
package com.liangjie.s01_e05_view;
import android.app.Activity;
import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
private Button button;
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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;
}
class ButtonListener implements OnClickListener {
@Override
public void onClick(View arg0) {
count ++;
textView.setTag(count + "");
}
}
@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);
textView = (TextView)rootView.findViewById(R.id.textView);
button = (Button)rootView.findViewById(R.id.button);
ButtonListener buttonListener = new ButtonListener();
button.setOnClickListener(buttonListener);
//textView.setText("Hello Jie");
//textView.setBackgroundColor(Color.BLUE);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
return rootView;
}
}
}
不知道你看懂了没
#3
@inquisitive_plus
我当时下载的是adt-bundle-windows-x86_64-20140321.zip,新建项目后默认给我打开的是fragment_main的编辑页面,我就在上面修改了,以为java文件里加载layout.activity_main会自动关联到fragment_main。后来换了版本直接出现的是activity_main,不存在fragment_main,就没问题了
感谢你的回答,让我明白了出错的原理!
我当时下载的是adt-bundle-windows-x86_64-20140321.zip,新建项目后默认给我打开的是fragment_main的编辑页面,我就在上面修改了,以为java文件里加载layout.activity_main会自动关联到fragment_main。后来换了版本直接出现的是activity_main,不存在fragment_main,就没问题了
感谢你的回答,让我明白了出错的原理!
#4
你声明的类MainActivity必须是继承FragmentActivity
不然就会出现这个错误
不然就会出现这个错误
#1
看logcat的错误就很明白了。
你的setContentView加载的是R.layout.activity_main
但是你的button的实际位置是R.layout.fragment_main
所以你用findviewbyid获得的是null,最后运行报的是空指针
只要把第24行改为setContentView(R.layout.fragment_main);
你的setContentView加载的是R.layout.activity_main
但是你的button的实际位置是R.layout.fragment_main
所以你用findviewbyid获得的是null,最后运行报的是空指针
只要把第24行改为setContentView(R.layout.fragment_main);
#2
package com.liangjie.s01_e05_view;
import android.app.Activity;
import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
private Button button;
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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;
}
class ButtonListener implements OnClickListener {
@Override
public void onClick(View arg0) {
count ++;
textView.setTag(count + "");
}
}
@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);
textView = (TextView)rootView.findViewById(R.id.textView);
button = (Button)rootView.findViewById(R.id.button);
ButtonListener buttonListener = new ButtonListener();
button.setOnClickListener(buttonListener);
//textView.setText("Hello Jie");
//textView.setBackgroundColor(Color.BLUE);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
return rootView;
}
}
}
不知道你看懂了没
#3
@inquisitive_plus
我当时下载的是adt-bundle-windows-x86_64-20140321.zip,新建项目后默认给我打开的是fragment_main的编辑页面,我就在上面修改了,以为java文件里加载layout.activity_main会自动关联到fragment_main。后来换了版本直接出现的是activity_main,不存在fragment_main,就没问题了
感谢你的回答,让我明白了出错的原理!
我当时下载的是adt-bundle-windows-x86_64-20140321.zip,新建项目后默认给我打开的是fragment_main的编辑页面,我就在上面修改了,以为java文件里加载layout.activity_main会自动关联到fragment_main。后来换了版本直接出现的是activity_main,不存在fragment_main,就没问题了
感谢你的回答,让我明白了出错的原理!
#4
你声明的类MainActivity必须是继承FragmentActivity
不然就会出现这个错误
不然就会出现这个错误