所以,我刚开始学习如何编写Java / Android代码 但我一直在制作这个真正简单的代码,但我好像被卡住了......
MainAcitivty.Java
package com.example.button;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
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.Button;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
int counter;
Button add, sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
display = (TextView) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter+=1;
display.setText("Your Total is "+ counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter-=1;
display.setText("Your Total is "+ counter);
}
});
}
}
@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;
}
}
}
fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_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.example.button.MainActivity$PlaceholderFragment" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Your Total is 0"
android:textSize="40dp"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/tvDisplay"
android:orientation="vertical"
/>
<Button
android:id="@+id/bSub"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvDisplay"
android:layout_below="@+id/tvDisplay"
android:hint="Subtract one"
android:textSize="20dp" />
<Button
android:id="@+id/bAdd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/bSub"
android:layout_below="@+id/bSub"
android:hint="Add one"
android:textSize="20dp" />
</RelativeLayout>
AndoridManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.button"
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.example.button.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>
</application>
</manifest>
错误我一直在
04-04 04:15:22.332: E/AndroidRuntime(1524): FATAL EXCEPTION: main
04-04 04:15:22.332: E/AndroidRuntime(1524): Process: com.example.button, PID: 1524
04-04 04:15:22.332: E/AndroidRuntime(1524): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.button/com.example.button.MainActivity}: java.lang.NullPointerException
04-04 04:15:22.332: E/AndroidRuntime(1524): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-04 04:15:22.332: E/AndroidRuntime(1524): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-04 04:15:22.332: E/AndroidRuntime(1524): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-04 04:15:22.332: E/AndroidRuntime(1524): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-04 04:15:22.332: E/AndroidRuntime(1524): at android.os.Handler.dispatchMessage(Handler.java:102)
04-04 04:15:22.332: E/AndroidRuntime(1524): at android.os.Looper.loop(Looper.java:136)
04-04 04:15:22.332: E/AndroidRuntime(1524): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-04 04:15:22.332: E/AndroidRuntime(1524): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 04:15:22.332: E/AndroidRuntime(1524): at java.lang.reflect.Method.invoke(Method.java:515)
04-04 04:15:22.332: E/AndroidRuntime(1524): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-04 04:15:22.332: E/AndroidRuntime(1524): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-04 04:15:22.332: E/AndroidRuntime(1524): at dalvik.system.NativeStart.main(Native Method)
04-04 04:15:22.332: E/AndroidRuntime(1524): Caused by: java.lang.NullPointerException
04-04 04:15:22.332: E/AndroidRuntime(1524): at com.example.button.MainActivity.onCreate(MainActivity.java:35)
04-04 04:15:22.332: E/AndroidRuntime(1524): at android.app.Activity.performCreate(Activity.java:5231)
04-04 04:15:22.332: E/AndroidRuntime(1524): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-04 04:15:22.332: E/AndroidRuntime(1524): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-04 04:15:22.332: E/AndroidRuntime(1524): ... 11 more
非常感谢他们的帮助:D
将Activity_main更改为Fragment
后出现的新错误04-04 04:44:46.902: E/AndroidRuntime(1575): FATAL EXCEPTION: main
04-04 04:44:46.902: E/AndroidRuntime(1575): Process: com.example.button, PID: 1575
04-04 04:44:46.902: E/AndroidRuntime(1575): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.button/com.example.button.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.button:id/container) for fragment PlaceholderFragment{b1dcdaa0 #0 id=0x7f05003c}
04-04 04:44:46.902: E/AndroidRuntime(1575): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-04 04:44:46.902: E/AndroidRuntime(1575): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-04 04:44:46.902: E/AndroidRuntime(1575): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-04 04:44:46.902: E/AndroidRuntime(1575): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-04 04:44:46.902: E/AndroidRuntime(1575): at android.os.Handler.dispatchMessage(Handler.java:102)
04-04 04:44:46.902: E/AndroidRuntime(1575): at android.os.Looper.loop(Looper.java:136)
04-04 04:44:46.902: E/AndroidRuntime(1575): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-04 04:44:46.902: E/AndroidRuntime(1575): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 04:44:46.902: E/AndroidRuntime(1575): at java.lang.reflect.Method.invoke(Method.java:515)
04-04 04:44:46.902: E/AndroidRuntime(1575): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-04 04:44:46.902: E/AndroidRuntime(1575): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-04 04:44:46.902: E/AndroidRuntime(1575): at dalvik.system.NativeStart.main(Native Method)
04-04 04:44:46.902: E/AndroidRuntime(1575): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.button:id/container) for fragment PlaceholderFragment{b1dcdaa0 #0 id=0x7f05003c}
04-04 04:44:46.902: E/AndroidRuntime(1575): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:930)
04-04 04:44:46.902: E/AndroidRuntime(1575): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
04-04 04:44:46.902: E/AndroidRuntime(1575): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-04 04:44:46.902: E/AndroidRuntime(1575): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
04-04 04:44:46.902: E/AndroidRuntime(1575): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
04-04 04:44:46.902: E/AndroidRuntime(1575): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
04-04 04:44:46.902: E/AndroidRuntime(1575): at android.app.Activity.performStart(Activity.java:5241)
04-04 04:44:46.902: E/AndroidRuntime(1575): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
04-04 04:44:46.902: E/AndroidRuntime(1575): ... 11 more
答案 0 :(得分:1)
您的EditText和命令按钮位于fragment_main.xml文件中,因此更改此行
setContentView(R.layout.activity_main);
到
setContentView(R.layout.fragment_main);
答案 1 :(得分:1)
你是什么
setContentView(R.layout.activity_main);
按钮添加是片段布局
<Button
android:id="@+id/bAdd"
因此,您的初始化失败导致NullPointerException
。
您需要将视图的初始化移至片段。
引起:java.lang.IllegalArgumentException:找不到id的视图 片段的0x7f05003c(com.example.button:id / container) PlaceholderFragment {b1dcdaa0#0 id = 0x7f05003c}
这清楚地表明您在设置为活动的布局中没有带有id容器的ViewGroup。
因为你有
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
答案 2 :(得分:0)
您的Button
,TextView
和所有其他UI
元素不在activity_main
中,但在fragment_main.xml
中。因此,请检查您的xml文件,并根据它查找所有UI
元素的ID。
您还必须致电
setContentView(R.layout.fragment_main);
而不是
setContentView(R.layout.activity_main);
答案 3 :(得分:0)
检查调用时使用的布局名称
setContentView(R.layout.activity_main);
对应于项目res/layout
文件夹
如上所述,布局似乎是fragment_main.xml
而不是activity_main*.xml
..
答案 4 :(得分:0)
问题是您正在尝试从活动中执行片段按钮的单击事件处理。 bAdd
和bSub
按钮属于片段的布局,但您尝试从活动中访问它们(通过调用活动上的findViewById
)。这些调用产生null
,因为它们不在活动的布局中。
正确的解决方法是将片段布局中按钮的处理移动到片段类。这就是使用片段的方式:可以在父活动中使用的自包含片段。因此,您应将大部分代码从MainActivity.onCreate
移至PlaceholderFragment.onActivityCreated
。
或者,如果您并未真正计划重用PlaceholderFragment
,则可以将其删除并将所有内容移动到活动的布局中。 不能将片段用于简单的用例。
答案 5 :(得分:0)
请确保在activity_main
布局中有{id} FrameLayout
的视图container
,如下所示:
<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....MainActivity"
tools:ignore="MergeRootFrame" />