你好,我刚刚开始编写代码,我构建了一个计数器应用程序,但是它既不在我的模拟器上也不在我的Android手机上运行。任何帮助请
这是xml代码(activity_main.xml)
<RelativeLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="16dp"
android:text="@string/team_a"
android:textColor="@color/colorAccent"
android:textSize="14sp"
app:fontFamily="cursive" />
<TextView
android:id="@+id/team_a_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="24dp"
android:text="@string/_0"
android:textColor="@color/colorAccent"
android:textSize="56sp"
app:fontFamily="cursive" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="8dp"
android:onClick="addThreeForTeamA"
android:text="@string/_3_points" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="8dp"
android:onClick="addTwoForTeamA"
android:text="@string/_2_points" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="8dp"
android:onClick="addOneForTeamA"
android:text="@string/free_throw" />
</LinearLayout>
<view
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:background="@android:color/holo_blue_bright" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="cursive"
android:gravity="center"
android:padding="16dp"
android:text="@string/team_b"
android:textColor="#FF1C25C8"
android:textSize="14sp" />
<TextView
android:id="@+id/team_b_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="cursive"
android:gravity="center"
android:paddingBottom="24dp"
android:text="@string/_0"
android:textColor="#FF1C25C8"
android:textSize="56sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="8dp"
android:onClick="addThreeForTeamB"
android:text="@string/_3_points" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="8dp"
android:onClick="addTwoForTeamB"
android:text="@string/_2_points" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="8dp"
android:onClick="addOneForTeamB"
android:text="@string/free_throw" />
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="8dp"
android:onClick="resetScore"
android:text="@string/reset" />
</RelativeLayout>
Java代码如下(MainActivity.java)
package com.example.monsurah.counter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int scoreTeamA = 0;
int scoreTeamB = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.example.monsurah.counter.R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
*Adds one for Team A
*/
public void addOneForTeamA (View v) {
scoreTeamA = scoreTeamA + 1;
displayForTeamA(scoreTeamA);
}
/**
*Adds two for Team A
*/
public void addTwoForTeamA (View v) {
scoreTeamA = scoreTeamA + 2;
displayForTeamA(scoreTeamA);
} /**
*Adds three for Team A
*/
public void addThreeForTeamA (View v) {
scoreTeamA = scoreTeamA + 3;
displayForTeamA(scoreTeamA);
} /**
*Adds one for Team B
*/
public void addOneForTeamB (View v) {
scoreTeamB = scoreTeamB + 1;
displayForTeamB(scoreTeamB);
} /**
*Adds two for Team B
*/
public void addTwoForTeamB (View v) {
scoreTeamB = scoreTeamB + 2;
displayForTeamB(scoreTeamB);
} /**
*Adds three for Team B
*/
public void addThreeForTeamB (View v) {
scoreTeamB = scoreTeamB + 3;
displayForTeamB(scoreTeamB);
}
/**
*Reset Scores
*/
public void resetScore (View v) {
scoreTeamA = 0;
scoreTeamB = 0;
displayForTeamB(scoreTeamB);
displayForTeamB(scoreTeamB);
}
/**
* Displays the given score for Team A.
*/
public void displayForTeamA(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_a_score);
scoreView.setText(String.valueOf(score));
}
/**
* Displays the given score for Team B.
*/
public void displayForTeamB(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_b_score);
scoreView.setText(String.valueOf(score));
}
}
我收到的错误消息是
原因:android.view.InflateException:二进制XML文件第68行: 尝试调用虚拟方法'boolean 空对象引用上的java.lang.String.equals(java.lang.Object)' 引起原因:java.lang.NullPointerException:尝试在其上调用虚拟方法'boolean java.lang.String.equals(java.lang.Object)' 空对象引用 在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:768) 在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734) 在android.view.LayoutInflater.rInflate(LayoutInflater.java:865) 在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828) 在android.view.LayoutInflater.rInflate(LayoutInflater.java:873) 在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828) 在android.view.LayoutInflater.inflate(LayoutInflater.java:525) 在android.view.LayoutInflater.inflate(LayoutInflater.java:427) 在android.view.LayoutInflater.inflate(LayoutInflater.java:378) 在android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 在android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 在com.example.monsurah.counter.MainActivity.onCreate(MainActivity.java:16) 在android.app.Activity.performCreate(Activity.java:6942) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3018) 在android.app.ActivityThread.-wrap14(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1653) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6724) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1520) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
答案 0 :(得分:0)
在布局中将view
更改为View
。
<view
android:layout_width="1dp"
... />
到
<View
android:layout_width="1dp"
... />
建议:有关一线解决方案,请参见@ myanswer。