我已经安装了最新版本的Android SDK和Eclipse,
Eclipse现在强制使用我以前工作过的片段和代码,现在根本不工作,事实上它在启动时崩溃了应用程序。
我创建了一个新的Android应用程序,在屏幕上放置了一个按钮,并尝试将代码链接到按钮并使用我在以前的应用程序中使用的setOnClickListener,然后我发现了一大堆错误我不知道如何解决。
在下面的代码中,按钮的setOnClickListener正在崩溃应用程序,如果我注释掉应用程序启动的监听器,但显然我没有代码背后的任何代码,所以它只是GUI。
我有一个Google,无法找到与我的问题相关的任何内容。
我哪里出错/我做错了什么?如何通过代码启动我的应用程序?
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.example.newandroidapplication.MainActivity"
tools:ignore="MergeRootFrame" />
fragment_main.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.newandroidapplication.MainActivity$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/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="18dp"
android:layout_marginTop="27dp"
android:text="Button" />
</RelativeLayout>
MainActivity.java
package com.example.newandroidapplication;
import android.support.v7.app.ActionBarActivity;
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.Toast;
public class MainActivity extends ActionBarActivity
{
private Button button1;
@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();
}
button1 = (Button)findViewById(R.id.button1);
// If I comment out the listener here, the app launches
button1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "I'm Working", Toast.LENGTH_LONG).show();
}
});
}
@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;
}
}
}
LogCat日志
04-03 13:58:29.223: D/AndroidRuntime(3351): Shutting down VM
04-03 13:58:29.223: W/dalvikvm(3351): threadid=1: thread exiting with uncaught exception (group=0xb0d8db20)
04-03 13:58:29.223: E/AndroidRuntime(3351): FATAL EXCEPTION: main
04-03 13:58:29.223: E/AndroidRuntime(3351): Process: com.example.newandroidapplication, PID: 3351
04-03 13:58:29.223: E/AndroidRuntime(3351): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newandroidapplication/com.example.newandroidapplication.MainActivity}: java.lang.NullPointerException
04-03 13:58:29.223: E/AndroidRuntime(3351): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-03 13:58:29.223: E/AndroidRuntime(3351): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-03 13:58:29.223: E/AndroidRuntime(3351): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-03 13:58:29.223: E/AndroidRuntime(3351): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-03 13:58:29.223: E/AndroidRuntime(3351): at android.os.Handler.dispatchMessage(Handler.java:102)
04-03 13:58:29.223: E/AndroidRuntime(3351): at android.os.Looper.loop(Looper.java:136)
04-03 13:58:29.223: E/AndroidRuntime(3351): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-03 13:58:29.223: E/AndroidRuntime(3351): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 13:58:29.223: E/AndroidRuntime(3351): at java.lang.reflect.Method.invoke(Method.java:515)
04-03 13:58:29.223: E/AndroidRuntime(3351): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-03 13:58:29.223: E/AndroidRuntime(3351): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-03 13:58:29.223: E/AndroidRuntime(3351): at dalvik.system.NativeStart.main(Native Method)
04-03 13:58:29.223: E/AndroidRuntime(3351): Caused by: java.lang.NullPointerException
04-03 13:58:29.223: E/AndroidRuntime(3351): at com.example.newandroidapplication.MainActivity.onCreate(MainActivity.java:33)
04-03 13:58:29.223: E/AndroidRuntime(3351): at android.app.Activity.performCreate(Activity.java:5231)
04-03 13:58:29.223: E/AndroidRuntime(3351): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-03 13:58:29.223: E/AndroidRuntime(3351): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-03 13:58:29.223: E/AndroidRuntime(3351): ... 11 more
答案 0 :(得分:3)
Eclipse现在强制使用片段
不,它没有强迫&#34;你要使用片段。 尝试以这种方式为您设置方便&#34;虽然我认为这取决于您的目标和最低API(我必须创建一个新的项目到确定,因为它已经有一段时间了。)
FrameLayout
,因为它在开始时为你设置了它。如果您想要使用片段,那么您只需要适当地引用Button
。
Button
位于片段布局中而不是{{1布局。但是您引用Activity
的方式,它正在查看id
布局。