MyApp包含1个TextView和2个按钮 - 添加和子。按下添加按钮时,计数器++。 sub - >反击 - 。
我从控制台获得了msg - 无法获得wglGetExtensionsStringARB。
可以看到主页。无法打开MyApp。需要强制关闭。
主:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.btp.myapp.MainActivity">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
/>
</menu>
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.btp.myapp"
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.btp.myapp.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>
MainActivity.java:
package com.btp.myapp;
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.TextView;
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);
counter = 0;
add = (Button) findViewById(R.id.add);
sub = (Button) findViewById(R.id.sub);
display = (TextView)findViewById(R.id.textView2);
add.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
// TODO Auto-generated method stub
counter++;
display.setText("Your total is" + counter);
}
});
sub.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
// TODO Auto-generated method stub
counter--;
display.setText("Your total is" + counter);
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().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;
}
@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的:
06-18 04:05:45.942: I/dalvikvm(325): Could not find method
android.content.pm.PackageManager.getActivityLogo, referenced from method
android.support.v7.internal.widget.ActionBarView.<init>
06-18 04:05:45.942: W/dalvikvm(325): VFY: unable to resolve virtual method 318: Landroid/content/pm/PackageManager;.getActivityLogo (Landroid/content/ComponentName;)Landroid/graphics/drawable/Drawable;
06-18 04:05:45.942: D/dalvikvm(325): VFY: replacing opcode 0x6e at 0x008b
06-18 04:05:45.951: I/dalvikvm(325): Could not find method android.content.pm.ApplicationInfo.loadLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
06-18 04:05:45.951: W/dalvikvm(325): VFY: unable to resolve virtual method 314: Landroid/content/pm/ApplicationInfo;.loadLogo (Landroid/content/pm/PackageManager;)Landroid/graphics/drawable/Drawable;
06-18 04:05:45.951: D/dalvikvm(325): VFY: replacing opcode 0x6e at 0x0099
06-18 04:05:45.972: D/dalvikvm(325): VFY: dead code 0x008e-0092 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
06-18 04:05:45.972: D/dalvikvm(325): VFY: dead code 0x009c-00a0 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
06-18 04:05:46.221: D/AndroidRuntime(325): Shutting down VM
06-18 04:05:46.221: W/dalvikvm(325): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-18 04:05:46.241: E/AndroidRuntime(325): FATAL EXCEPTION: main
06-18 04:05:46.241: E/AndroidRuntime(325): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.btp.myapp/com.btp.myapp.MainActivity}: java.lang.NullPointerException
06-18 04:05:46.241: E/AndroidRuntime(325): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-18 04:05:46.241: E/AndroidRuntime(325): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-18 04:05:46.241: E/AndroidRuntime(325): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-18 04:05:46.241: E/AndroidRuntime(325): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-18 04:05:46.241: E/AndroidRuntime(325): at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 04:05:46.241: E/AndroidRuntime(325): at android.os.Looper.loop(Looper.java:123)
06-18 04:05:46.241: E/AndroidRuntime(325): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-18 04:05:46.241: E/AndroidRuntime(325): at java.lang.reflect.Method.invokeNative(Native Method)
06-18 04:05:46.241: E/AndroidRuntime(325): at java.lang.reflect.Method.invoke(Method.java:521)
06-18 04:05:46.241: E/AndroidRuntime(325): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-18 04:05:46.241: E/AndroidRuntime(325): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-18 04:05:46.241: E/AndroidRuntime(325): at dalvik.system.NativeStart.main(Native Method)
06-18 04:05:46.241: E/AndroidRuntime(325): Caused by: java.lang.NullPointerException
06-18 04:05:46.241: E/AndroidRuntime(325): at com.btp.myapp.MainActivity.onCreate(MainActivity.java:32)
06-18 04:05:46.241: E/AndroidRuntime(325): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-18 04:05:46.241: E/AndroidRuntime(325): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-18 04:05:46.241: E/AndroidRuntime(325): ... 11 more
答案 0 :(得分:0)
日志引用MainActivity中第32行抛出的空引用异常。
这就是这条线 add.setOnClickListener(new View.OnClickListener(){
现在从该行加载了对象'add' add =(Button)findViewById(R.id.add);
查看你的xml文件,我看不到你有一个id为'add'的资源,这可能解释了它。因此无法找到您的资源,因此'add'为空