简体   繁体   中英

Android app crashing and can't find why

so I've been looking on here for a solution/reason on my app crashing. Additionally my logcat won't work. When it did work, it reported something wrong with the main activity, but I provided all the code. If anyone can help me with this it will be greatly appreciated.

mainActivity:

     import android.os.Bundle;
     import android.app.Activity;
     import android.view.Menu;
     import android.view.View;
     import android.widget.*;
     import android.widget.AdapterView.OnItemClickListener;


 public class MainActivity extends Activity { 
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final GridView gridview = (GridView) findViewById(R.id.gridView1);
        Table t=new Table((Double) null, null);
        gridview.setAdapter(new ImageAdapter(this, t));

        gridview.setOnItemClickListener(new OnItemClickListener() 
        {
             @Override
             public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
             {
                 Toast.makeText(gridview.getContext(), "" + position, Toast.LENGTH_SHORT).show();
             }

        });
   }


}

ImageAdapter:

package com.example.myspending;

import java.util.ArrayList;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

public class ImageAdapter extends BaseAdapter 
{
    private Context mContext;
    private ArrayList<String> texts;
    public ImageAdapter(Context context, Table t) 
    {
        mContext=context;
        for(int i=0; i<t.size(); i++)
        {
        texts.add((t.table.get(i).name)+" $"+(t.table.get(i).money));
        }
    }
    @Override
    public int getCount() 
    {
        return texts.size();
    }
    @Override
    public Object getItem(int position) 
    {
         return null;
    }
    @Override
    public long getItemId(int position) {
        return 0;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView tv;
        if(convertView==null){
            tv=new TextView(mContext);
            tv.setLayoutParams(new GridView.LayoutParams(85,85));
        }
        else{
            tv=(TextView)convertView;
        }
        tv.setText(texts.get(position));
        tv.setTextSize(30);
        return tv;
    }

}

Table:

package com.example.myspending;

import java.util.*;

public class Table
{
        public ArrayList<spending> table= new ArrayList<spending>();
    double price;
    String name;
    double total;
    public Table(double p, String n)
    {
        price=p;
        name=n;
        total+=price;
        table.add(new spending(total, "Total"));
        table.add(new spending(price, name));
    }
    public void insert(double p, String n)
    {
        total+=p;
        table.add(new spending(p, n));
    }
    public void delete(String n)
    {
       for(int i=0; i<table.size(); i++)
       {
            if(table.get(i).name==n)
            {
                total-=table.get(i).money;
                table.remove(i);
            }
       }
    }
    public void reset()
    {
        for(int i=0; i<table.size(); i++)
        {
                table.remove(i);
        }
    }
    public int size()
    {
        return table.size();
    }
}
class spending
{
    public double money;
    public String name;
    public spending(double m, String n)
    {
        money=m;
        name=n;
           if(!(name=="deposit"||name=="Deposit"||name=="add"||name=="Add"||name=="added"||name=="Added"))
        {
            money=money*(-1);
        }
    }
 }

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.myspending"
     android:versionCode="1"
     android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.myspending.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>

Layout:

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >



    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="325sp"
        android:layout_toRightOf="@+id/textView1"
        android:numColumns="3" >

    </GridView>

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/gridView1"
        android:ems="10"
        android:inputType="text" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/gridView1"
        android:layout_below="@+id/editText1"
        android:text="add" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignLeft="@+id/editText1"
        android:ems="10"
        android:inputType="numberDecimal" />

</RelativeLayout>
class spending
{
    public double money;
    public String name;

    public spending(double m, String n)
    {
        money=m;
        name=n;

        if(name.equalsIgnoreCase("deposit") == false && 
           name.equalsIgnoreCase("add") == false  && 
           name.equalsIgnoreCase("added") == false)
        {
            money *= -1;
        }
    }
}

Take out your (Double) null , what are you trying to accomplish there? Also, use the item's context, not gridview 's

public class MainActivity extends Activity
{ 
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        final GridView gridview = (GridView) findViewById(R.id.gridView1);

        Table t = new Table(-1d, null);

        gridview.setAdapter(new ImageAdapter(this, t));

        gridview.setOnItemClickListener(new OnItemClickListener() 
        {
             public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
             {
                 Toast.makeText(v.getContext(), Integer.toString(position), Toast.LENGTH_SHORT).show();
             }
        });
   }
}

A few general remarks:

  1. Posting code in such a big extent makes it hard to analyze for others, so your chances for getting the Really Useful Answer(tm) diminish.
  2. In the course of reducing your code to a short, self contained, compilable example (see http://sscce.org/ ) you will make it also easier for yourself to fence off the problem. It often happens at this stage that the author finds the flaw by himself.
  3. If the problem is reproducible, ie it happens at every run – this is a good news; as you need less runs to pin it down
  4. Use debugging tools; if you do not have/know a debugger, simply outputting intermediate results or even simple messages like 'milestone 1 reached' will enable you to delimit the part of the program where the error manifests itself
  5. Starting writing the code from scratch (what may come to your mind in despair) is not good long-term, as even if you get to a working code, you will never learn what was wrong here and how to deal with programming errors in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM