致命异议:主要? Android的 [英] FATAL EXCEPTION: main? Android

查看:44
本文介绍了致命异议:主要? Android的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MainActivity.java



  package  com.example.ecomm ; 

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends 活动{
/ * *首次创建活动时调用。 * /
@覆盖
public void onCreate(Bundle savedInstanceState){
super .onCreate(savedInstanceState);
setContentView(R.layout.main);

final LinearLayout layout =(LinearLayout)findViewById(R.id.linearMain);
final 按钮btn =(按钮)findViewById(R.id.second);

final Controller ct =(Controller)getApplicationContext();


ModelProducts products = null;

for int i = 1 ; i< = 7; i ++){

int 价格= 15+ i;

products = new ModelProducts( 产品项目 + i, 描述 + i,Price);

ct.setProducts(products);
}


int productsize = ct.getProductArraylistsize();

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);


for int j = 0; j< productsize ; j ++){


String pName = ct.getProducts(j).getProductName();
int pPrice = ct.getProducts(j).getProductPrice();


LinearLayout la = new LinearLayout( this );

la.setOrientation(LinearLayout.HORIZONTAL);

TextView tv = new TextView( this );

tv.setText( + pName + );

la.addView(tv);

TextView tv1 = new TextView( this );

tv1.setText( $ + pPrice + );

la.addView(tv1);

final 按钮btn1 = new 按钮();

btn1.setId(j + 1);
btn1.setText( 添加到购物车);


btn1.setLayoutParams(params);

final int index = j;

btn1.setOnClickListener( new OnClickListener(){

public void onClick(查看v){
// TODO自动生成的方法存根


Log.i( TAG index: + index);


ModelProducts productsObject = ct.getProducts(index);


if (! ct.getCart()。CheckProductInCart(productsObject)){

btn1.setText( Item添加了);

ct.getCart()。setProducts(productsObject);

Toast.makeText(getApplicationContext(), New CartSize: + ct.getCart()。getCartsize(),Toast.LENGTH_LONG)。show();

} else {

Toast.makeText(getApplicationContext(), Products +(index + 1)+ 已添加,Toast.LENGTH_LONG)。show();





}

}

});

la.addView(btn1);

layout.addView(la);


}

btn.setOnClickListener( new OnClickListener(){

@覆盖
public void onClick(查看v){
// TODO自动生成的方法存根

意图in = new Intent(getBaseContext(),Screen2。 class );
startActivity(in);



}
});

}
}







main.xml



 <   linearlayout     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
tools:context =。MainActivity
android:orientation =vertical

>

< textview > ;
android:layout_width =wrap_removed
android:layout_height =wrap_removed
android:layout_centerHorizo​​ntal =true
android:layout_centerVertical =true
android:textColor =#898989
android:textStyle =bold
android:textSize =18sp
android:text =GreenBerry/>

< linearlayout > ;
android:layout_width =match_parent
android:layout_height =wrap_content
android:orientation =vertical
android:id =@ + id / linearMain
>

< / linearlayout >
< 按钮 android:id = @ + id / second

android:layout_width = wrap_content

android:layout_height = wrap_content

< span class =code-attribute> android:text = 结帐

/ >

解决方案

+ pPrice + );

la.addView(tv1);

final 按钮btn1 = new 按钮();

btn1.setId(j + 1);
btn1.setText( 添加到购物车);


btn1.setLayoutParams(params);

final int index = j;

btn1.setOnClickListener( new OnClickListener(){

public void onClick(查看v){
// TODO自动生成的方法存根


Log.i( TAG index: + index);


ModelProducts productsObject = ct.getProducts(index);


if (! ct.getCart()。CheckProductInCart(productsObject)){

btn1.setText( Item添加了);

ct.getCart()。setProducts(productsObject);

Toast.makeText(getApplicationContext(), New CartSize: + ct.getCart()。getCartsize(),Toast.LENGTH_LONG)。show();

} else {

Toast.makeText(getApplicationContext(), Products +(index + 1)+ 已添加,Toast.LENGTH_LONG)。show();





}

}

});

la.addView(btn1);

layout.addView(la);


}

btn.setOnClickListener( new OnClickListener(){

@覆盖
public void onClick(查看v){
// TODO自动生成的方法存根

意图in = new Intent(getBaseContext(),Screen2。 class );
startActivity(in);



}
});

}
}







main.xml



 <   linearlayout     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
tools:context =。MainActivity
android:orientation =vertical

>

< textview > ;
android:layout_width =wrap_removed
android:layout_height =wrap_removed
android:layout_centerHorizo​​ntal =true
android:layout_centerVertical =true
android:textColor =#898989
android:textStyle =bold
android:textSize =18sp
android:text =GreenBerry/>

< linearlayout > ;
android:layout_width =match_parent
android:layout_height =wrap_content
android:orientation =vertical
android:id =@ + id / linearMain
>

< / linearlayout >
< 按钮 android:id = @ + id / second

android:layout_width = wrap_content

android:layout_height = wrap_content

< span class =code-attribute> android:text = 结帐

/ >


MainActivity.java

package com.example.ecomm;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
final LinearLayout layout = (LinearLayout)findViewById(R.id.linearMain);
final Button btn = (Button)findViewById(R.id.second);

final Controller ct = (Controller) getApplicationContext();
        
        
 ModelProducts products = null; 
 
         for(int i= 1; i<=7;i++){
        	 
        	  int Price = 15+ i;
        	  
        	  products = new ModelProducts("Product Item" +i, "Description"+i, Price);
        	 
        	  ct.setProducts(products);
         }
        
         
         int productsize = ct.getProductArraylistsize();
         
         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
         
         
         for (int j=0;j< productsize;j++){
        	 
        	 
        	String pName = ct.getProducts(j).getProductName(); 
        	int pPrice = ct.getProducts(j).getProductPrice();
        	
        	
        	 LinearLayout la = new LinearLayout(this);
        	
        	 la.setOrientation(LinearLayout.HORIZONTAL);
        	    
        	 TextView tv = new TextView(this);
        	 
        	 tv.setText(" "+pName+" ");
        	 
        	 la.addView(tv);
        	    
             TextView tv1 = new TextView(this);
        	 
        	 tv1.setText("$"+pPrice+" ");
        	 
        	 la.addView(tv1);
        	 
        	  final Button btn1 = new Button(this);
        	  
        	               btn1.setId(j+1);
        	               btn1.setText("Add to Cart");
        	               
        	     
        	        btn1.setLayoutParams(params);
        	        
        	   final int index = j;
        	   
        	   btn1.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
					
					Log.i("TAG", "index:"+index);
					
					
					ModelProducts productsObject = ct.getProducts(index);
					
					
					if(!ct.getCart().CheckProductInCart(productsObject)){
					
					btn1.setText("Item Added");
					
					ct.getCart().setProducts(productsObject);
							
					Toast.makeText(getApplicationContext(), "New CartSize:" +ct.getCart().getCartsize(),Toast.LENGTH_LONG).show();		
							
					}else{
						
				    Toast.makeText(getApplicationContext(), "Products"+(index+1)+"Already Added",Toast.LENGTH_LONG ).show();
						
						 
						
						
						
					   }
							
				}	
					
		});
        	               
        	 la.addView(btn1);
        	 
        	 layout.addView(la);
        	   
        	   
          }
         
         btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				Intent in = new Intent(getBaseContext(),Screen2.class);
				 startActivity(in);
				
				
				
			}
		});
        
    }
}




main.xml

<linearlayout 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"
    tools:context=".MainActivity" 
    android:orientation="vertical"
    
    >
    
    <textview>
        android:layout_width="wrap_removed"
        android:layout_height="wrap_removed"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textColor="#898989"
        android:textStyle="bold"
        android:textSize="18sp"
        android:text="GreenBerry" />
    
    <linearlayout>
        android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:orientation="vertical"
	    android:id="@+id/linearMain"
	    >
	    
     </linearlayout>   
    <Button android:id="@+id/second"

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="Checkout"

	/>

解决方案

"+pPrice+" "); la.addView(tv1); final Button btn1 = new Button(this); btn1.setId(j+1); btn1.setText("Add to Cart"); btn1.setLayoutParams(params); final int index = j; btn1.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Log.i("TAG", "index:"+index); ModelProducts productsObject = ct.getProducts(index); if(!ct.getCart().CheckProductInCart(productsObject)){ btn1.setText("Item Added"); ct.getCart().setProducts(productsObject); Toast.makeText(getApplicationContext(), "New CartSize:" +ct.getCart().getCartsize(),Toast.LENGTH_LONG).show(); }else{ Toast.makeText(getApplicationContext(), "Products"+(index+1)+"Already Added",Toast.LENGTH_LONG ).show(); } } }); la.addView(btn1); layout.addView(la); } btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent in = new Intent(getBaseContext(),Screen2.class); startActivity(in); } }); } }




main.xml

<linearlayout 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"
    tools:context=".MainActivity" 
    android:orientation="vertical"
    
    >
    
    <textview>
        android:layout_width="wrap_removed"
        android:layout_height="wrap_removed"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textColor="#898989"
        android:textStyle="bold"
        android:textSize="18sp"
        android:text="GreenBerry" />
    
    <linearlayout>
        android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:orientation="vertical"
	    android:id="@+id/linearMain"
	    >
	    
     </linearlayout>   
    <Button android:id="@+id/second"

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="Checkout"

	/>


这篇关于致命异议:主要? Android的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆