Android - 尝试更改活动时出错

时间:2014-12-11 22:14:12

标签: java android

出于某种原因,在尝试切换活动时出现了这种情况。我在此项目中与其他活动运行相同类型的代码没有任何问题。我会发布代码,看看是否有人能搞清楚;(

我无法在此格式化错误,所以这里是错误日志 http://textuploader.com/oe6g

这是我的不同活动。

当我点击我的新游戏活动布局中的按钮时出现问题,该按钮运行确认(查看视图)

NewGameActivity.java

public class NewGameActivity extends Activity {
EditText title;
TextView team1, team2;
SqliteHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_newgame);
    //ListViews
    team1 = (TextView) findViewById(R.id.textView4);
    team2 = (TextView) findViewById(R.id.textView5);
    title = (EditText) findViewById(R.id.editText1);
    db = new SqliteHelper(this.getApplicationContext());
    populateListViews();
    registerClickCallback();
}

/*
 * 
 * Database Functions
 * 
 */





/*
 * 
 * ListView Stuff
 * 
 */
private void populateListViews() {
    // THIS HERE WILL POPULATE BOTH TEAM LIST VIEWS
    //Create list of items
    Cursor cursor = db.getTeams();

    ArrayList<String> values = new ArrayList<String>();
    if (cursor != null && cursor.getCount() != 0) {
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {

            values.add(cursor.getString(cursor.getColumnIndex("Team_Names")));

            cursor.moveToNext();
        }
    }       
    //Build Adapter
    ArrayAdapter<String> t1adapter = new ArrayAdapter<String>(
            this,                   // Context
            R.layout.teamlistviews,     // Layout to use
            values);                // Items to be displayed
    ArrayAdapter<String> t2adapter = new ArrayAdapter<String>(
            this,                   // Context
            R.layout.teamlistviews,     // Layout to use
            values);        
    //Configure the List View
    ListView t1list = (ListView) findViewById(R.id.listView1);
    t1list.setAdapter(t1adapter);
    ListView t2list = (ListView) findViewById(R.id.listView2);
    t2list.setAdapter(t2adapter);
}
private void registerClickCallback() {
    //This uses the List View and adds a listener to check for clicks/taps on different
    //list view items. It will then display a message telling you which one you have selected.
    ListView t1list = (ListView) findViewById(R.id.listView1);
    ListView t2list = (ListView) findViewById(R.id.listView2);
    t1list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View viewClicked,
                int position, long id) {
            //team 1
                TextView textView = (TextView) viewClicked;

                //Changing team name
                TextView t1 = (TextView) findViewById(R.id.textView4);
                String team1 = textView.getText().toString();
                t1.setText(team1);
                //Toast message
                String message = "You Selected " + textView.getText().toString() + " for Team 1";
                Toast.makeText(NewGameActivity.this, message, Toast.LENGTH_SHORT).show();
        }

    });
    t2list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View viewClicked,
                int position, long id) {
            //team 2
                TextView textView = (TextView) viewClicked;
                //Changing Team name
                TextView t2 = (TextView) findViewById(R.id.textView5);
                String team2 = textView.getText().toString();
                t2.setText(team2);
                //Toast message
                String message = "You Selected " + textView.getText().toString() + " for Team 2";
                Toast.makeText(NewGameActivity.this, message, Toast.LENGTH_SHORT).show();
        }

    });
}
public void confirm(View view) {
    String t1, t2, gTitle;
    t1 = team1.toString();
    t2 = team2.toString();
    gTitle = title.toString();

    Intent intent = new Intent(this, CourtActivity.class);
    intent.putExtra("TEAM1", t1);
    intent.putExtra("TEAM2", t2);
    intent.putExtra("GAME_TITLE", gTitle);
    startActivity(intent);

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.new_game, 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);
}
}

CourtActivity.java

package com.example.statapalpha;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.Math;
import java.util.ArrayList;

// Court Screen
public class CourtActivity extends Activity implements OnMenuItemClickListener{

// Opens database
SqliteHelper db;

ArrayList<String> homePlayersIn = new ArrayList<String>();
ArrayList<String> awayPlayersIn = new ArrayList<String>();
ArrayList<String> homePlayersBench = new ArrayList<String>();
ArrayList<String> awayPlayersBench = new ArrayList<String>();

String team1, team2, team1n, team2n;
String player = "0"; // Player number for current play
String action = ""; // Action text for current play
position position = new position(); // Position for current play
int playNumber = 0;
private PopupMenu popupMenu;
boolean isHome = false;
int playerButton = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_court);
    db = new SqliteHelper(this.getApplicationContext());
    //Get Team 1 and 2 and Game Title
    convertStrings();
    // Gets players
    getPlayers();
}
public void convertStrings() {
    Intent mIntent = getIntent();
    team1n = mIntent.getStringExtra("TEAM1");
    team2n = mIntent.getStringExtra("TEAM2");
}
// Populates arrays with player numbers
void getPlayers() {
    homePlayersBench.add("12");
    homePlayersBench.add("10");
    homePlayersBench.add("32");
    homePlayersBench.add("16");
    homePlayersBench.add("13");
    homePlayersBench.add("19");

    homePlayersIn.add("13");
    homePlayersIn.add("14");
    homePlayersIn.add("15");
    homePlayersIn.add("16");
    homePlayersIn.add("17");

    awayPlayersBench.add("21");
    awayPlayersBench.add("22");
    awayPlayersBench.add("23");
    awayPlayersBench.add("24");
    awayPlayersBench.add("25");
    awayPlayersBench.add("26");

    awayPlayersIn.add("31");
    awayPlayersIn.add("32");
    awayPlayersIn.add("33");
    awayPlayersIn.add("34");
    awayPlayersIn.add("35");
}

// Stores x and y coordinate
public class position {
    public int x;
    public int y;
}

// Sets current player when a player button is clicked
public void setPlayer(View v) {
    Button b = (Button)v;
    player = b.getText().toString();
    playerButton = b.getId();
    switch(b.getId()) {
    case R.id.p1: 
    case R.id.p2:
    case R.id.p3:
    case R.id.p4:
    case R.id.p5: isHome = true; break;
    default: isHome = false;
            break;
    }
}

// Sets string Action to whatever action the user taps
// then records play to database.
public void setAction(View v) {
    Button b = (Button)v;
    String toastAction = "";
    String message = "";
    String team = "";

    switch(b.getId()) {
    case R.id.fgMade: action = "F" + goal(position) + "H"; toastAction = "made " + goal(position) + " point shot";
    break;
    case R.id.fgMissed: action = "F" + goal(position) + "M"; toastAction = "missed " + goal(position) + " point shot";
    break;
    case R.id.ftMade: action = "FTH"; toastAction = "made freethrow";
    break;
    case R.id.ftMissed: action = "FTM"; toastAction = "missed freethrow";
    break;
    case R.id.rebound: action = "RB"; toastAction = "rebound";
    break;
    case R.id.assist: action = "AST"; toastAction = "assist";
    break;
    case R.id.block: action = "BL"; toastAction = "block";
    break;
    case R.id.steal: action = "STL"; toastAction = "steal";
    break;
    case R.id.turnover: action = "TO"; toastAction = "turnover";
    break;
    case R.id.sub: action = "SUB"; toastAction = "substitution";
    break;
    case R.id.foul: action = "FC"; toastAction = "commited foul";
    break;
    case R.id.undoPlay: undoPlay(v);
    break;
    }

    if (action == "SUB") {
        popupMenu = new PopupMenu(this.getBaseContext(), v);
        Menu menu = popupMenu.getMenu();

        if (isHome == true) {
            for (String number : homePlayersBench) {
                menu.add(number);
            }
        }
        else {
            for (String number : awayPlayersBench) {
                    menu.add(number);
                }
        }

        popupMenu.setOnMenuItemClickListener(this);
        popupMenu.show();

        return;
    }
    else {
        message = "Player " + player + " " + toastAction + " at ("  + Integer.toString(position.x) + ", " + Integer.toString(position.y) + ")";
    }

    if (isHome == true)
        team = team1;
    else
        team = team2;

    playNumber++;
    db.recordPlay(Integer.parseInt(player), team, action, position, playNumber);
    Toast.makeText(CourtActivity.this, message, Toast.LENGTH_SHORT).show();
    refreshPlayers();
}


// Gets tap position and saves it to 'position'
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_UP) {
        position.x = (int)event.getX(0);
        position.y = (int)event.getY(0);
    }

    return super.onTouchEvent(event);
}

int goal(position position) {
    int points=3;

    if ((Math.sqrt(Math.pow((position.x - 1082), 2) + Math.pow((position.y - 453), 2)) < 270) || (Math.sqrt(Math.pow((position.x - 193), 2) + Math.pow((position.y - 453), 2)) < 270)) points = 2;

    return points;
}


public void undoPlay(View v) {

    db.undoPlay(Integer.toString(playNumber));
}



@Override
public boolean onMenuItemClick(MenuItem item) {
    Button button = (Button)findViewById(playerButton);
    button.setText(item.getTitle());
    refreshPlayers();
    return false;
}
}

activity_court.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:background="@drawable/bg2court"
tools:context="${relativePackage}.${activityClass}" xmlns:android1="http://schemas.android.com/apk/res/android">

<Button
    android:id="@+id/editPlays"
    android:layout_width="190dp"
    android:layout_height="75dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:onClick="editPlays"
    android:text="@string/edit_plays"
     />

<Button
    android:id="@+id/undoPlay"
    android:layout_width="190dp"
    android:layout_height="75dp"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/editPlays"
    android:onClick="undoPlay"
    android:text="@string/undo" />

<Button
    android:id="@+id/ftMissed"
    android:layout_width="190dp"
    android:layout_height="85dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:onClick="setAction"
    android:text="@string/miss" />

<Button
    android:id="@+id/ftMade"
    android:layout_width="190dp"
    android:layout_height="85dp"
    android:layout_above="@+id/ftMissed"
    android:layout_alignParentRight="true"
    android:onClick="setAction"
    android:text="@string/made" />

<Button
    android:id="@+id/fgMade"
    android:layout_width="190dp"
    android:layout_height="85dp"
    android:layout_above="@+id/fgMissed"
    android:layout_alignParentRight="true"
    android:onClick="setAction"
    android:text="@string/made" />

<Button
    android:id="@+id/fgMissed"
    android:layout_width="190dp"
    android:layout_height="85dp"
    android:layout_above="@+id/ftMade"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="52dp"
    android:onClick="setAction"
    android:text="@string/miss" />

<TextView
    android:id="@+id/fgText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/fgMade"
    android:layout_alignLeft="@+id/playerStats"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:text="@string/field_goal"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="38sp" />

<Button
    android:id="@+id/playerStats"
    android:layout_width="190dp"
    android:layout_height="75dp"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/undoPlay"
    android:onClick="redoPlay"
    android:text="@string/playerStats" />

<TextView
    android:id="@+id/ftText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/ftMade"
    android:layout_alignRight="@+id/fgText"
    android1:layout_alignLeft="@+id/ftMade"
    android:text="@string/free_throw"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="34sp" />

<Button
    android:id="@+id/foul"
    android:layout_width="190dp"
    android:layout_height="80dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/ftMade"
    android:onClick="setAction"
    android:color= "#FF0079FF"
    android:text="@string/foul" />

<Button
    android:id="@+id/steal"
    android:layout_width="190dp"
    android:layout_height="80dp"
    android:layout_above="@+id/turnover"
    android:layout_alignParentLeft="true"
    android:onClick="setAction"
    android:text="@string/steal" />

<Button
    android:id="@+id/block"
    android:layout_width="190dp"
    android:layout_height="80dp"
    android:layout_above="@+id/steal"
    android:layout_alignParentLeft="true"
    android:onClick="setAction"
    android:text="@string/block" />

<Button
    android:id="@+id/assist"
    android:layout_width="190dp"
    android:layout_height="80dp"
    android:layout_above="@+id/block"
    android:layout_alignParentLeft="true"
    android:onClick="setAction"
    android:text="@string/assist" />

<Button
    android:id="@+id/rebound"
    android:layout_width="190dp"
    android:layout_height="80dp"
    android:layout_above="@+id/assist"
    android:layout_alignParentLeft="true"
    android:onClick="setAction"
    android:text="@string/rebound" />


<Button
    android:id="@+id/p1"
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/rebound"
    android:onClick="setPlayer"
    android:text="@string/p1" />

<Button
    android:id="@+id/p2"
    android:layout_width="85dp"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/p1"
    android:onClick="setPlayer"
    android:text="@string/p2" />

<Button
    android:id="@+id/p3"
    android:layout_width="85dp"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/p2"
    android:onClick="setPlayer"
    android:text="@string/p3" />

<Button
    android:id="@+id/p4"
    android:layout_width="85dp"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/p3"
    android:onClick="setPlayer"
    android:text="@string/p4" />

<Button
    android:id="@+id/p5"
    android:layout_width="85dp"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/p4"
    android:onClick="setPlayer"
    android:text="@string/p5" />

<Button
    android:id="@+id/p10"
    android:layout_width="85dp"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/editPlays"
    android:onClick="setPlayer"
    android:text="@string/p10" />

<Button
    android:id="@+id/p9"
    android:layout_width="85dp"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/p10"
    android:onClick="setPlayer"
    android:text="@string/p9" />

<Button
    android:id="@+id/p8"
    android:layout_width="85dp"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/p9"
    android:onClick="setPlayer"
    android:text="@string/p8" />

<Button
    android:id="@+id/p7"
    android:layout_width="85dp"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/p8"
    android:onClick="setPlayer"
    android:text="@string/p7" />

<Button
    android:id="@+id/p6"
    android:layout_width="85dp"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/p7"
    android:onClick="setPlayer"
    android:text="@string/p6" />

<Button
    android:id="@+id/sub"
    android:layout_width="190dp"
    android:layout_height="80dp"
    android:layout_above="@+id/foul"
    android:layout_toLeftOf="@+id/p1"
    android:onClick="setAction"
    android:text="@string/sub" />

<Button
    android:id="@+id/turnover"
    android:layout_width="190dp"
    android:layout_height="80dp"
    android:layout_above="@+id/sub"
    android:layout_toLeftOf="@+id/p1"
    android:onClick="setAction"
    android:text="@string/turnover" />

<TextView
    android:id="@+id/points"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/court"
    android:layout_alignRight="@+id/rebound"
    android:text="@string/points"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/fouls"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/undoPlay"
    android:layout_toLeftOf="@+id/p1"
    android:text="@string/fouls"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/p1p"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/points"
    android:layout_alignRight="@+id/p1"
    android:layout_marginRight="35dp"
    android:text="@string/p1p"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/p1f"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/fouls"
    android:layout_alignRight="@+id/p1"
    android:layout_marginRight="35dp"
    android:text="@string/p1f"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/p2p"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/points"
    android:layout_alignRight="@+id/p2"
    android:layout_marginRight="35dp"
    android:text="@string/p2p"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/p2f"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/fouls"
    android:layout_alignRight="@+id/p2"
    android:layout_marginRight="35dp"
    android:text="@string/p2f"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/p3p"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/points"
    android:layout_alignRight="@+id/p3"
    android:layout_marginRight="35dp"
    android:text="@string/p3p"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/p3f"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/fouls"
    android:layout_alignRight="@+id/p3"
    android:layout_marginRight="35dp"
    android:text="@string/p3f"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/p4p"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/points"
    android:layout_alignRight="@+id/p4"
    android:layout_marginRight="35dp"
    android:text="@string/p4p"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/p4f"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/fouls"
    android:layout_alignRight="@+id/p4"
    android:layout_marginRight="35dp"
    android:text="@string/p4f"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/p5p"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/points"
    android:layout_alignRight="@+id/p5"
    android:layout_marginRight="35dp"
    android:text="@string/p5p"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/p5f"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/fouls"
    android:layout_alignRight="@+id/p5"
    android:layout_marginRight="35dp"
    android:text="@string/p5f"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/p6p"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/points"
    android:layout_alignRight="@+id/p6"
    android:layout_marginRight="35dp"
    android:text="@string/p6p"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/p6f"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/fouls"
    android:layout_alignRight="@+id/p6"
    android:layout_marginRight="35dp"
    android:text="@string/p6f"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/p7p"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/points"
    android:layout_alignRight="@+id/p7"
    android:layout_marginRight="35dp"
    android:text="@string/p7p"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/p7f"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/fouls"
    android:layout_alignRight="@+id/p7"
    android:layout_marginRight="35dp"
    android:text="@string/p7f"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/p8p"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/points"
    android:layout_alignRight="@+id/p8"
    android:layout_marginRight="35dp"
    android:text="@string/p8p"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/p8f"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/fouls"
    android:layout_alignRight="@+id/p8"
    android:layout_marginRight="35dp"
    android:text="@string/p8f"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/p9p"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/points"
    android:layout_alignRight="@+id/p9"
    android:layout_marginRight="35dp"
    android:text="@string/p9p"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/p9f"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/fouls"
    android:layout_alignRight="@+id/p9"
    android:layout_marginRight="35dp"
    android:text="@string/p9f"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/p10p"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/points"
    android:layout_alignRight="@+id/p10"
    android:layout_marginRight="35dp"
    android:text="@string/p10p"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/p10f"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/fouls"
    android:layout_alignRight="@+id/p10"
    android:layout_marginRight="35dp"
    android:text="@string/p10f"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<ImageView
    android:id="@+id/court"
    android:layout_width="900dp"
    android:layout_height="600dp"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/p10"
    android:contentDescription="@string/undo"
    android:src="@drawable/court" />

</RelativeLayout>

我的意思是它告诉我在CourtActivity的onCreate中发生了错误,但我无法看到任何错误。这是我学校项目的一部分,我只关心当前导致我错误的事情,而不是太担心别的事情。

注意:这是一个小组项目。 courtactivity屏幕是我唯一没有参与制作的屏幕,因此我不确定究竟是什么进入了它。但是现在我试图把所有东西放在一起并遇到这个问题。

一切都有帮助!谢谢!

3 个答案:

答案 0 :(得分:0)

错误来自于布局的膨胀。这是一个OutOfMemoryError。你有一些非常大的位图或更可能是循环依赖吗?你能上传xml文件R.layout.activity_court吗?

答案 1 :(得分:0)

尝试加载布局文件中指定的图像时发生错误,如日志的以下部分所示:

...: Caused by: java.lang.OutOfMemoryError
...: E/AndroidRuntime(2137):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
...: E/AndroidRuntime(2137):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:503)

我敢打赌,如果从文件activity_court.xml中的根容器(RelativeLayout)中删除行android:background="@drawable/bg2court",则错误将消失。 如果是这种情况,我会建议您尝试该图像的较小(较低分辨率)版本 在寻找替代品时,请密切关注logcat上的堆内存分配消息,以便了解您正在取得多大进展。

在Android上加载高分辨率图像通常是一个棘手的过程,原因如下:

  1. 内存中加载的位图文件必须解压缩,以便可以渲染它们,使它们比原始的.JPG和.PNG文件大得多。要计算将分配多少字节的内存来加载给定图像,只需遵循以下简单公式:width( pixels )x height( pixels )x每个像素的数字字节(24位图像为3)。示例:大小为2048x2048像素的位图只占用12mb的内存;
  2. 位图被加载到堆空间中,这恰好是操作系统在启动时为应用程序提供的相当小的内存部分。堆空间的大小通常在16到64mb之间,具体取决于设备。
  3. 最后,我建议采用以下方法之一:

    1. 用较低分辨率的版本替换大背景图片,该版本可以按比例放大以填充背景空间(以代码的质量为代价);
    2. 以编程方式加载位图。 BitmapFactory方法可以在加载过程中动态下采样图像。有关此here;
    3. 的更多信息
    4. 使用第三方库为您完成。 Picasso是一个极好的选择。

答案 2 :(得分:0)

我不久前遇到了这个错误,但是从这里的人那里得到了一些帮助

这是我做的事情

如果你的背景/图像不需要转发东西,或者像JPG格式一样,因为它会更小。这应该排除你的内存不足问题。但是当你有更多的图像出现时,你仍然会有问题(内存不足)再次出现

希望这可以帮助您暂时