我已经在互联网和网站上搜索了足够的答案。我也复制粘贴了本网站上几个已经发布的答案的代码,但没有任何帮助。我的代码中没有编译时错误。因此,我创造了一个新问题..
我正在使用 Android Studio 0.4.4 。我的完整代码如下。请告诉我哪里出错了。它在模拟器和我的HTC设备上发出运行时错误“抱歉应用程序已意外停止”。
package com.sample.controls;
import android.app.Activity;
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.os.Build;
import android.widget.Button;
import android.content.DialogInterface;
import android.content.Context;
import android.app.AlertDialog;
public class MainActivity extends ActionBarActivity {
protected Button btnDialog;
final Context context = this;
@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();
}
btnDialog = (Button)findViewById(R.id.btnDialog);
btnDialog.setOnClickListener(ClickListenerDelegate);
}
@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;
}
}
private View.OnClickListener ClickListenerDelegate = new View.OnClickListener() {
@Override
public void onClick(View v){
if(v == MainActivity.this.btnDialog){
/* Design Alert Dialog */
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder
.setTitle("New Dialog Box")
.setMessage("Click OK to exit the app")
.setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface di, int which) {
MainActivity.this.finish();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface di, int which) {
di.cancel();
}
});
/* Create and show the Alert Dialog physically on screen */
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
};
}
下面附有日志猫:
02-17 20:35:58.547 30846-30846/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.sample.controls/com.sample.controls.MainActivity}:
java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2517) at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574)
at android.app.ActivityThread.access$600(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1413)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:5789)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.sample.controls.MainActivity.onCreate(MainActivity.java:32)
at android.app.Activity.performCreate(Activity.java:5195)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2473)
附件是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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.sample.controls.MainActivity$PlaceholderFragment">
<TextView
android:text="@string/strMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#f4b02f"
android:gravity="center_vertical|center_horizontal" android:id="@+id/txtMessage"
android:textColor="#750f12" android:textStyle="bold" android:textSize="24dp"
android:typeface="sans" android:layout_margin="0dp" android:layout_alignParentRight="false"
android:layout_alignParentBottom="false"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Dialog"
android:id="@+id/btnDialog"
android:layout_marginTop="32dp" android:layout_below="@+id/txtMessage"
android:layout_alignParentLeft="true" android:layout_alignParentStart="true"
android:textColor="#f4b02f" android:typeface="sans" android:textStyle="bold"
android:background="#71152b" android:width="200dp"/>
</RelativeLayout>
附件是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.sample.controls.MainActivity"
tools:ignore="MergeRootFrame" />
答案 0 :(得分:-1)
您需要在onCreate
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this; // initialize
因为你在下面的代码中使用它
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
另外
private View.OnClickListener ClickListenerDelegate = new View.OnClickListener() {
@Override
public void onClick(View v){
switch(v.getId())
{
case R.id.btnDialog:
// dialog button clicked
break;
case : R.id.secondbutton:
// second button clicked
break:
}
}
};
从您的评论可能btnDialog
为空。确保正确初始化
按钮处于片段布局中。所以你需要在fragment的onCreate
中初始化按钮@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
btnDialog = (Button)rootView.findViewById(R.id.btnDialog);
btnDialog.setOnClickListener(ClickListenerDelegate);
return rootView;
}