我只是android编程的初学者,目前我正在android中创建一个简单的项目。但是我面临很多错误。我几乎没有疑问:
1)每当我创建一个项目时,它创建一个两个布局 activity_main.xml和fragment_main.xml 我知道什么是片段但我很困惑哪一个使用......!有人说只需使用 fragment_main.xml 来设计所有活动。这是对的吗?或者我还需要使用 activity_main ?
2)我创建了一个应用程序,它首先显示闪存屏幕,然后进入活动但是当我运行应用程序时,它显示不幸的是应用程序停止了,代码是
SplashActivity:
public class SplashActivity extends Activity {
private static int SPLASH_TIME_OUT = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_splash);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashActivity.this, AboutUs.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}}
关于我们:
public class AboutUs extends Activity implements OnClickListener {
TextView tv_au1;
TextView tv_woff;
Button btn_go;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_us);
tv_au1=(TextView) findViewById(R.id.textView1);
tv_woff=(TextView) findViewById(R.id.textView2);
btn_go=(Button) findViewById(R.id.button1);
btn_go.setOnClickListener(this);
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.about_us, 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_about_us,
container, false);
return rootView;
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent go_create_note = new Intent(this, Dashboard.class);
startActivity(go_create_note);
}}
我的项目中没有任何错误,我的logcat是:
05-08 13:00:05.790: E/AndroidRuntime(847): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-08 13:00:05.790: E/AndroidRuntime(847): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-08 13:00:05.790: E/AndroidRuntime(847): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-08 13:00:05.790: E/AndroidRuntime(847): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-08 13:00:05.790: E/AndroidRuntime(847): at android.os.Handler.dispatchMessage(Handler.java:102)
05-08 13:00:05.790: E/AndroidRuntime(847): at android.os.Looper.loop(Looper.java:136)
05-08 13:00:05.790: E/AndroidRuntime(847): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-08 13:00:05.790: E/AndroidRuntime(847): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 13:00:05.790: E/AndroidRuntime(847): at java.lang.reflect.Method.invoke(Method.java:515)
05-08 13:00:05.790: E/AndroidRuntime(847): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-08 13:00:05.790: E/AndroidRuntime(847): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-08 13:00:05.790: E/AndroidRuntime(847): at dalvik.system.NativeStart.main(Native Method)
05-08 13:00:05.790: E/AndroidRuntime(847): Caused by: java.lang.NullPointerException
05-08 13:00:05.790: E/AndroidRuntime(847): at com.example.notedip.AboutUs.onCreate(AboutUs.java:32)
05-08 13:00:05.790: E/AndroidRuntime(847): at android.app.Activity.performCreate(Activity.java:5231)
05-08 13:00:05.790: E/AndroidRuntime(847): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-08 13:00:05.790: E/AndroidRuntime(847): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-08 13:00:05.790: E/AndroidRuntime(847): ... 11 more
任何人都能解释一下吗?非常感谢提前
答案 0 :(得分:0)
我猜视图属于fragment_about_us.xml
,在这种情况下你需要在Fragment中初始化视图
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_about_us,
container, false);
tv_au1=(TextView) rootView.findViewById(R.id.textView1);
tv_woff=(TextView) rootView.findViewById(R.id.textView2);
btn_go=(Button) rootView.findViewById(R.id.button1);
btn_go.setOnClickListener(this);
return rootView;
}
更改此
public static class PlaceholderFragment extends Fragment {
到
public static class PlaceholderFragment extends Fragment implements OnClickListener{
然后将下面的内容移到片段类
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent go_create_note = new Intent(getActivity(), Dashboard.class);
startActivity(go_create_note);
}