我在manifest.xml中更改了android标签
这是myactivity中的代码
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
为什么会这样?
我使用Android 5.0 API 21
这是我的日志猫
日期&amp; Time.571:D / AndroidRuntime(399):关闭VM
日期&amp; Time.571:W / dalvikvm(399):threadid = 1:线程退出,未捕获异常(组= 0x40015560)
日期&amp; Time.590:E / AndroidRuntime(399):致命异常:主要
日期&amp; Time.590:E / AndroidRuntime(399):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.abc/com.example.abc.MainActivity}:java.lang.NullPointerException
日期&amp; Time.590:E / AndroidRuntime(399):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
日期&amp; Time.590:E / AndroidRuntime(399):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
日期&amp; Time.590:E / AndroidRuntime(399):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
日期&amp; Time.590:E / AndroidRuntime(399):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
日期&amp; Time.590:E / AndroidRuntime(399):在android.os.Handler.dispatchMessage(Handler.java:99)
日期&amp; Time.590:E / AndroidRuntime(399):在android.os.Looper.loop(Looper.java:123)
日期&amp; Time.590:E / AndroidRuntime(399):在android.app.ActivityThread.main(ActivityThread.java:3683)
日期&amp; Time.590:E / AndroidRuntime(399):at java.lang.reflect.Method.invokeNative(Native Method)
日期&amp; Time.590:E / AndroidRuntime(399):at java.lang.reflect.Method.invoke(Method.java:507)
日期&amp; Time.590:E / AndroidRuntime(399):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
日期&amp; Time.590:E / AndroidRuntime(399):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
日期&amp; Time.590:E / AndroidRuntime(399):at dalvik.system.NativeStart.main(Native Method)
日期&amp; Time.590:E / AndroidRuntime(399):引起:java.lang.NullPointerException
日期&amp; Time.590:E / AndroidRuntime(399):at com.example.abc.MainActivity.onCreate(MainActivity.java:57)
日期&amp; Time.590:E / AndroidRuntime(399):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
日期&amp; Time.590:E / AndroidRuntime(399):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
日期&amp; Time.590:E / AndroidRuntime(399):... 11更多
日期&amp; Time.690:I / Process(399):发送信号。 PID:399 SIG:9
这是我在模拟器上收到的错误消息 抱歉! 应用程序abc(进程com.example.abc)意外停止。请再试一次。
我正在使用Nexus S (4.0", 480 x 800:hdpi) Target Android 2.3.3 - API Level 10
作为我的Android虚拟设备而我是not able to change the Target to Android 5.0 - API Level 21
因为“确定”按钮被禁用,它说&#34;未选择CPU / ABI系统映像&#34 ;
package com.example.abc;
import android.app.Activity;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.Window;
import java.io.Console;
import java.io.IOException;
import android.app.ActionBar;
import java.io.*;
import java.util.*;
import android.view.Menu;
public class MainActivity extends Activity
{
LinearLayout layout1, buttonLayout;
EditText number1Text, number2Text;
Button calcButton, addButton, subButton, divideButton, multiplyButton;
TextView answerText, bannerText, result;
//ActionBar actionBar = getSupportActionBar();
//actionBar.hide();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
layout1 = new LinearLayout(this);
number1Text = new EditText(this);
number2Text = new EditText(this);
addButton = new Button(this);
multiplyButton = new Button(this);
divideButton = new Button(this);
answerText = new TextView(this);
answerText.setPadding(0, 20, 0, 0);
answerText.setGravity(Gravity.CENTER_HORIZONTAL);
//layout1.setOrientation(LinearLayout.VERTICAL);
layout1.setGravity(Gravity.CENTER_HORIZONTAL);
//buttonLayout.setOrientation(LinearLayout.HORIZONTAL);
buttonLayout.setGravity(Gravity.CENTER_HORIZONTAL);
number1Text.setInputType(InputType.TYPE_CLASS_NUMBER);
number2Text.setInputType(InputType.TYPE_CLASS_NUMBER);
answerText.setWidth(150);
answerText.setGravity(Gravity.CENTER_HORIZONTAL);
answerText.setText("0");
addButton.setText("+");
calcButton.setText("Click to Calculate");
addButton.setOnClickListener(addClicked);
layout1.addView(bannerText);
layout1.addView(number1Text);
layout1.addView(number2Text);
layout1.addView(calcButton);
layout1.addView(buttonLayout);
layout1.addView(answerText);
setContentView(layout1);
number1Text.setLayoutParams(new LinearLayout.LayoutParams(100,50));
number2Text.setLayoutParams(new LinearLayout.LayoutParams(100,50));
addButton.setLayoutParams(new LinearLayout.LayoutParams(50,50));
}
private OnClickListener addClicked = new OnClickListener()
{
@Override
public void onClick(View v){
String firstString = number1Text.getText().toString();
String secondString = number2Text.getText().toString();
double firstNumber = Double.parseDouble(firstString);
double secondNumber = Double.parseDouble(secondString);
double result = firstNumber + secondNumber;
String resultString = String.valueOf(result);
answerText.setText(resultString);
}
};
}
这是我最新的日志猫
日期&amp; Time.705:D / AndroidRuntime(333):关闭VM
日期&amp; Time.705:W / dalvikvm(333):threadid = 1:线程退出,未捕获异常(组= 0x40015560)
日期&amp; Time.725:E / AndroidRuntime(333):致命异常:主要
日期&amp; Time.725:E / AndroidRuntime(333):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.abc/com.example.abc.MainActivity}:java.lang.NullPointerException
日期&amp; Time.725:E / AndroidRuntime(333):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
日期&amp; Time.725:E / AndroidRuntime(333):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
日期&amp; Time.725:E / AndroidRuntime(333):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
日期&amp; Time.725:E / AndroidRuntime(333):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
日期&amp; Time.725:E / AndroidRuntime(333):在android.os.Handler.dispatchMessage(Handler.java:99)
日期&amp; Time.725:E / AndroidRuntime(333):在android.os.Looper.loop(Looper.java:123)
日期&amp; Time.725:E / AndroidRuntime(333):在android.app.ActivityThread.main(ActivityThread.java:3683)
日期&amp; Time.725:E / AndroidRuntime(333):at java.lang.reflect.Method.invokeNative(Native Method)
日期&amp; Time.725:E / AndroidRuntime(333):at java.lang.reflect.Method.invoke(Method.java:507)
日期&amp; Time.725:E / AndroidRuntime(333):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
日期&amp; Time.725:E / AndroidRuntime(333):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
日期&amp; Time.725:E / AndroidRuntime(333):at dalvik.system.NativeStart.main(Native Method)
日期&amp; Time.725:E / AndroidRuntime(333):引起:java.lang.NullPointerException
日期&amp; Time.725:E / AndroidRuntime(333):at com.example.abc.MainActivity.onCreate(MainActivity.java:63)
日期&amp; Time.725:E / AndroidRuntime(333):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
日期&amp; Time.725:E / AndroidRuntime(333):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
日期&amp; Time.725:E / AndroidRuntime(333):... 11 more
如果有人能够让我知道为什么他们投票赞成这个答案没用,我感激不尽。我已经重写了代码,在这里它适用于其他所有人。
package com.example.calculate;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity
{
LinearLayout layout1;
EditText number1Text;
EditText number2Text;
Button calcButton;
TextView answerText;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
layout1 = new LinearLayout(this);
number1Text = new EditText(this);
number2Text = new EditText(this);
calcButton = new Button(this);
answerText = new TextView(this);
layout1.setOrientation(LinearLayout.VERTICAL);
answerText.setText("0");
calcButton.setText("Press to Calculate");
calcButton.setOnClickListener(addClicked);
layout1.addView(number1Text);
layout1.addView(number2Text);
layout1.addView(calcButton);
layout1.addView(answerText);
setContentView(layout1);
}
private OnClickListener addClicked = new OnClickListener()
{
@Override
public void onClick(View v){
String firstString = number1Text.getText().toString();
String secondString = number2Text.getText().toString();
double firstNumber = Double.parseDouble(firstString);
double secondNumber = Double.parseDouble(secondString);
double result = firstNumber + secondNumber;
String resultString = String.valueOf(result);
answerText.setText(resultString);
}
};
}
谢谢
答案 0 :(得分:0)
您正在尝试在null ButtonView上设置文本,即
calcButton.setText("Click to Calculate");
首先创建该按钮的对象,然后在其上设置文本。
答案 1 :(得分:0)
您的$sql = ("SELECT title FROM publication");
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
$data = array();
while($row = $result->fetch_assoc()){
$data[] = json_encode($row);
$titles = json_encode($data);
echo $titles;//for testing
}
file_put_contents('titles.json', $titles);
永远不会被初始化。这就是你得到空指针异常的原因
at
buttonLayout